//控制器中的方法
if(isset($_POST['Category']))
{
$model->attributes=$_POST['Category'];
if($model->save())
{
$this->redirect(array('view','id'=>$model->id));
}
}
//model文件
public function aftersave()
{
parent::afterSave();
$this->parentid = (int)substr($this->path,strripos($this->path,',')+1);
$this->path .= ','.$this->id;
self::save();
}
执行update显示执行insert.:
CDbCommand 无法执行 SQL 语句: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘121’ for key ‘PRIMARY’. The SQL statement executed was: INSERT INTO category
(parentid
, name
, path
, id
) VALUES (:yp0, :yp1, :yp2, :yp3)
不使用aftersave
//控制器
$model->attributes=$_POST['Category'];
if($model->save())
{
$model->parentid = (int)substr($model->path,strripos($model->path,',')+1);
$model->path .= $model->id.',';
$model->save();
$this->redirect(array('view','id'=>$model->id));
}
成功执行了update操作