Hope I got this right
The automatically generated actionUpdate code actually loads a model before updating it and reloads it again after update when the ‘show’ action is executed as follows:
[indent]
public function actionUpdate()
{
$model=$this->loadaddressbook();
if(isset($_POST['addressbook']))
{
$model->attributes=$_POST['addressbook'];
if($model->save())
$this->redirect(array('show','id'=>$model->id));
}
$this->render('update',array('model'=>$model));
}
[/indent]
Why not use the updateByPk as below. This only loads the model once when actually 'show’ing it.
[indent]
public function actionUpdate()
{
if(isset($_POST['addressbook']))
{
if(addressbook::model()->updateByPk ($_GET['id'], $_POST['addressbook']))
$this->redirect(array('show','id'=>$_GET['id']));
}
$this->render('update',array('model'=>$this->loadaddressbook()));
}
[/indent]
Same thing for the actionDelete. Why do we load a model if it is going to be deleted as follows:
[indent]
public function actionDelete()
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$this->loadaddressbook()->delete();
$this->redirect(array('list'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
[/indent]
This can be done without actually loading the model using the deleteByPk
[indent]
public function actionDelete()
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
addressbook::deleteByPk($_POST['id']);
$this->redirect(array('list'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
[/indent]
As a side note, while the deleteByPk can be called as a statis ‘::deleteByPk’ function, the updateByPk function throws a AddressbookController does not have a method named “getCommandBuilder” exception when called statically ‘::updateByPk’
/Kaf