在我的應用程序中,Comment模型部分在Product視圖中呈現。 除了在我的評論表中添加一個新列後,我無法將數據保存到新列(名爲'ddate')中,所有內容都是正常的。 即使我試過這個:Yii:無法將數據保存到新添加的列
$_POST['Comment']['ddate'] = 'something';
$model2->attributes=$_POST['Comment'];
$model2->save();
但不工作!
在我的應用程序中,Comment模型部分在Product視圖中呈現。 除了在我的評論表中添加一個新列後,我無法將數據保存到新列(名爲'ddate')中,所有內容都是正常的。 即使我試過這個:Yii:無法將數據保存到新添加的列
$_POST['Comment']['ddate'] = 'something';
$model2->attributes=$_POST['Comment'];
$model2->save();
但不工作!
$temp = $model->attributes;
$model->setFields($_POST['Comment'];);
$model->ddate = $_POST['Comment']['ddate'];
if($model->save())
{
echo "saved";
}
您必須在模型中將屬性設置爲「安全」,否則當調用$model2->attributes = $_POST['Comment'];
時,表格值不會被複制到模型中。
例子:
public function rules() {
return array(
array('ddate', 'safe'),
);
}
如果你總是希望ddate爲當前日期/時間,它可能是更好的設置它在beforeSave()
代替:
protected function beforeSave() {
if($this->hasAttribute('ddate') && !isset($this->ddate))
$this->ddate = date("Y-m-d H:i:s");
也許您尚未將此新屬性正確添加到模型中,使用gii預覽模型,並檢查差異 – tinybyte
我做了行b y線!沒有結果! – pooria
請發佈您的完整型號代碼 – tinybyte