1

In Yii multimodel form we just used actionCreate() to create the form of two models in a single view.Ok everything is fine upto here.But when we will update the two models in a single view of multimodel how the models will be defined here? Let me give you one example.Just think the database is just like this

 === Project ===
  id
  task_id(FK)
  description

  === Task ===
  id
  name
  description

So In actionCreate() of the project controller,the code will be something like this

    public function actionCreate()
  {
    $model=new Projects;
    $tasks=new Projects;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if (isset($_POST['Projects'],$_POST['Tasks']))
    {
      $model->attributes = $_POST['Projects'];
      $tasks->attributes = $_POST['Tasks'];
      $valid = $model->validate();
      $valid = $tasks->validate();
      if($valid)
      {
        $model->save(false);
        $tasks->save(false);
        $this->redirect(array('view','id'=>$model->id));
      }
    }
    $this->render('create',array(
      'model'=>$model,
      'tasks'=>$tasks,
    ));
  }

Now here the both models are ready for create. So what to do in actionView() and actionUpdate()?How to declare the both models?Any help and suggestions will be highly appriciable.

tereško
  • 58,060
  • 25
  • 98
  • 150
NewUser
  • 12,713
  • 39
  • 142
  • 236

2 Answers2

0

You didn't create any object for Task model.

AndRaGhu
  • 161
  • 7
  • 17
0

Is this really different? When loading view / update you'll just need to have ID's in GET which tells you which models to load. Models will then be Projects::model()->findByPk($myId) if using ActiveRecord. When updating you can assign attributes as you do with create, but make sure the model is loaded from the database first.

Eirik Hoem
  • 1,310
  • 7
  • 14