php - Yii insert record instead of update -
i'm using yii framework regarding php code.
i have 3 methods in controller, 1 creating, 1 updating, 1 saving. creating , updating render same form.
everything works fine saving record. when updated record, creates new record. in update method, load existing record , correct value, when save saves in new record. i'm try $model->update() instead of $model->save(), got error cannot update new record.
thank in advance insights
method create
$model = new comment; $this->renderpartial('_formcomment',array( 'model'=>$model, 'post_id'=>$post_id,) );
method update
$model=$this->loadmodel($comment_id); $this->renderpartial('_formcomment',array( 'model'=>$model, 'post_id'=>$model->post_id, 'comment_id'=>$model->id,) );
method save
$comment=new comment; if(isset($_post['comment'])) { $model->attributes=$_post['comment']; if($model->save()) { echo ('save ok'); } }
my loadmodel function called update method
if($this->_model===null) { $this->_model=comment::model()->findbypk($comment_id); if($this->_model===null) throw new chttpexception(404,'the requested page not exist.'); } return $this->_model;
you have
$comment=new comment;
in save method, create new model. need load existing model, set attributes , save it. pass $model in save method.
Comments
Post a Comment