actionUpdate on Multi Model form
How can I update through a multimodel form in Yii?
If my create action is
public function actionCreate()
{
$model = new Model;
$model2 = new Model2;
$model3 = new Model3;
Normally in update action I will use
public function actionUpdate($id)
{
$mode开发者_Python百科l = $this->loadModel($id, 'Model');
But how to I handle the others models (Model2 and Model3). I want to be able to update all models at the same time.
Did you update your "loadModel" method to use different models, something like:
public function myLoadModel($id, $m='Model')
{
$model=$m::model()->findByPk((int)$id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
And use that for your update functions.
I had the same problem. This is how I solved it:
public function loadModel($id)
{
$model=Model::model()->with('model2', 'model3',...)->findByPk((int)$id);
if($model===null)
throw new CHttpException(404,'Page not found.');
return $model;
}
Hope it to help you.
精彩评论