2013年2月7日 星期四

Yii 抽象資料庫欄位類型的支援



Yii Migration 支援多種抽象欄位類型,如下所示:
  • pk: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY"
  • string: string type, will be converted into "varchar(255)"
  • text: a long string type, will be converted into "text"
  • integer: integer type, will be converted into "int(11)"
  • boolean: boolean type, will be converted into "tinyint(1)"
  • float: float number type, will be converted into "float"
  • decimal: decimal number type, will be converted into "decimal"
  • datetime: datetime type, will be converted into "datetime"
  • timestamp: timestamp type, will be converted into "timestamp"
  • time: time type, will be converted into "time"
  • date: date type, will be converted into "date"
  • binary: binary data type, will be converted into "blob"


2013年1月31日 星期四

整合 Mantis 與 Gitolite

前陣子整合了公司的 mantis 和 gitolite,主要的目的就是要讓開發人員 push commits 到 gitolite 的時候,能夠同時更新 mantis 的 issue ,方便持續追蹤。

要完成這樣的功能,關鍵就在 git 的 post hooks,完整架構如下:
整合 Mantis 與 Gitolite

如上圖所示,當 gitolite 中的 git repo 收到開發人員的 commits 時,post hooks 會被呼叫,對應到的 hook 檔是 post-receive,與 svn 的 post-commit 不同,如下圖所示:
Post hook 的過程

瞭解了大概的架構之後,開始著手進行 post-receive 的撰寫。Gitolite 共用的 hooks 檔案位於 "/usr/share/gitolite/hooks/common/*",找到我們要的 post-receive 後開始編輯。
# 讀取輸入的物件檔案
while read oldrev newrev ref
do
    # 取得 commit 資訊
    subj=`git show -s --format="%s" $newrev`
    diff=`git show $newrev...$oldrev`
    msgs="$subj
$diff"
    # 將資訊餵給用來跟 mantis 同步的 perl 程式
    /home/gitolite/git2mantis.pl "$msgs"
    ...
done

這段 post hook 的目的是將收到的 git commit 資訊讀出來,然後餵給一隻 perl 程式來進行處理。撰寫完這個 hook ,還必須執行 gl-setup,這樣才會把這個 hook 放到每一個 git repo 下。

接下來,來看看 "/home/gitolite/git2mantis.pl" 做了什麼事情。
#!/usr/bin/perl
#
# 使用者 mantis 的 ssh key 必須在 192.168.26.103 主機上設定好,
# 才能允許自動登入。
$host = "mantis@192.168.0.1";

$sshcmd = "/usr/bin/ssh ";
$phpcmd = "/usr/bin/php";

# mantis 的 checkin 程式
$checkincmd = "/var/www/mantis/scripts/checkin.php";

$msg = $ARGV[0];

# 透過 ssh 來執行遠端主機的程式
`$sshcmd $host $phpcmd $checkincmd <<< "$msg"`;

這隻 perl 程式的工作就是把收到的訊息,透過 ssh 的方式,傳送給遠端的程式並且執行。

就這樣,完成了簡易的 mantis 和 gitolite 的整合,可以使用 mantis 來追蹤 git 的開發過程了。

2013年1月28日 星期一

繼承 Yii Framework 中的 AR 類別

關聯式資料庫並不支援繼承,但是,我們可以藉由繼承 AR 的方式來產生子類別繼承。什麼時候會需要繼承 AR 呢?最常見的情況應該是使用者分角色的時候,

例如:網站的使用者,區分為管理員、一般和進階使用者。不管哪一種角色都使用同一個資料表,並藉由繼承 User AR 的方式來產生各自的 AR。

Yii 官方維基有一篇介紹文章,使用 Car AR 來繼承產生 Sport Car 和 Family Car。寫得簡單明瞭,可以參考看看:
http://www.yiiframework.com/wiki/198/single-table-inheritance/

2013年1月24日 星期四

Git 有沒有像 SVN Export 一樣的功能?

答案是,沒有。

在 Git 中,沒有 export 這樣的指令來將程式碼輸出。

但是,Git 有提供封存(archive)的功能,稍微作點變化就可以達到像 svn export 一樣的功用。如下所示:
# 將當前狀態封存,並取出至指定位置
$ git archive HEAD | tar -x -C /path/to/export