例如(這是我的問題目的的一個壞例子),我想通過連接名字和姓氏來存儲用戶全名。 我有一個名字,姓氏,全名創建時自動更新的數據庫字段或使用symfony更新時自動更新的數據庫字段
有一個表用戶有一種方法來創建一個字段在架構中的觸發器功能,存儲在全名firstname +姓氏?在created_at廠(timestampable)用同樣的方法
我使用原則和symfony的1.4
例如(這是我的問題目的的一個壞例子),我想通過連接名字和姓氏來存儲用戶全名。 我有一個名字,姓氏,全名創建時自動更新的數據庫字段或使用symfony更新時自動更新的數據庫字段
有一個表用戶有一種方法來創建一個字段在架構中的觸發器功能,存儲在全名firstname +姓氏?在created_at廠(timestampable)用同樣的方法
我使用原則和symfony的1.4
你肯定不需要創建簡單和不可再因爲這東西的行爲。
只需重寫你的模型保存方法:
class Model extends BaseModel
{
public function save(Doctrine_Connection $conn)
{
$this->fullname = sprintf ('%s %s', $this->firstname, $this->lastname);
return parent::save($conn);
}
}
Dziamid已經直接回答你的要求。但也要考慮你是否真的需要存儲這樣的重複數據。我使用的一個不同的解決方案是:
class Model extends BaseModel
{
public function getFullName() {
return ($this->name_first . ' ' . $this->name_last);
}
public function getFullNameLastFirst() {
return ($this->name_last . ', ' . $this->name_first);
}
}
一種方法可以寫我自己的教義行爲。它在這裏解釋:http://www.symfony-project.org/more-with-symfony/1_4/en/08-Advanced-Doctrine-Usage。但也許有一個更簡單的解決方案 – 2011-05-18 15:46:23