Using action Update() in Yii to update another model's update

I have the database like this




 === Invoice ===

 id

 customer_id (FK)

 description


 === Customer ===

 id

 firstname

 lastname



I have multimodel for both the form so that Cstomer table will be load in Invoice. So that I can easily access the two models from a single view. For that I have made relation in both models just like this In Invoice model the realtion is like this




  public function relations()

  {

    return array(

    'customer'    =>  array(self::BELONGS_TO,'Customer','customer_id'),

    );

  }



In Customer Model the relation is like this




public function relations()

  {

    return array(

      'invoice' => array(self::HAS_MANY, 'Invoices','customer_id')

    );

  }



Everything is working fine.But when I am going for actionUpdate() in Invoice controller file there is Customer model is not defined. So I made it define like this




 public function actionView($id)

  {

    $this->render('view',array(

      'model'=>$this->loadModel($id),

      'customers'=>Customers::model()->findByPk(array('customer_id'=>$_GET['id']));

    ));

  }



It is showing as Undefined offset: 0. I want here in (‘customer_id’=>$_GET[‘id’]) the value of id so that I can easily show and update the values for each ids. If I am giving the value like this




public function actionView($id)

  {

    $this->render('view',array(

      'model'=>$this->loadModel($id),

      'customers'=>Customers::model()->findByPk(28);

    ));

  }



It is easily showing the value from Customer id. So how to get those values?Any help and suggestions will be highly appriciable.

First, in the view action of Invoice, you can access the customer information like …




// controller

$this->render('view',array('model'=>$this->loadModel($id)));

...

// view

<?php echo 'Customer = ' . $model->customer->first_name . ' ' . $model->customer->last_name; ?>



There’s no need to manually retrieve the customer instance, because you have established the relation.

And in the update action of Invoice, if you only need to show the customer information, then it’s just the same. You only have to access the customer information via ‘$model->customer->…’.

But when you want to update the customer information in the update action of Invoice, it will need some more coding …




// controller

$model = $this->loadModel($id); // invoice

$customer = Customers::model()->findByPk($model->customer_id); // customer

...

$this->render('update', array('model'=>$model, 'customer'=>$customer));

// view

...



I can’t show the codes in detail … You also need to consider the create action of Invoice, because “create” and “update” actions share the same _form.php by default.

I think this wiki will be helpful. How to use a single form to collect data for two or more models?

To update multiple models in a single form you have to change your loadModel in the controller.


public function loadModel($id)

{

        $model=MainModel::model()->with('relation')->findByPk($id);

        if($model===null)

                throw new CHttpException(404,'The requested page does not exist.');

        return $model;

}

and in your action this should be at the start


$model = $this->loadModel($id, 'MainModel');

$relatedModel = $model->relation;    OR   $model->relation->relatedAttribute;