1. 程式人生 > >Yii 自動設定模型的欄位值

Yii 自動設定模型的欄位值

[php] <?php class MasterAdmin extends CActiveRecord { public function beforeSave() { if($this->isNewRecord) // 只有在新建記錄時執行 { if($this->hasAttribute('createdDate')) // 如果模型有 createDate 欄位 $this->createdDate = new CDbExpression('NOW()'); // 設定 createdDate 的值 if($this->hasAttribute('createdIp')) // 如果模型有 createdIp 欄位 $this->createdIp = CHttpRequest::getUserHostAddress(); // 設定使用者的 IP } if($this->hasAttribute('updatedDate')) // 如果模型有 updatedDate 欄位 $this->updatedDate = new CDbExpression('NOW()'); // 設定 updatedDate 的值 if($this->hasAttribute('updatedIp')) // 如果模型有 updatedIp 欄位 $this->updatedIp = CHttpRequest::getUserHostAddress(); // 設定使用者的 IP return parent::beforeSave(); } } ?>