View problem when using multiple models

Hello All,

Is there any way to render an update view using various models, where some of them might not have records for a specific ID?

My database has two tables: Shop and ShopDetails. Data to be stored in Shop is mandatory whereas data in ShopDetails is optional, that means that some IDs from table Shop may not appear in table ShopDetails. The requirement is to update both tables using one form and only one button. So, in the controller I have:




$model = Shop::model()->with('shopDetailsRelation')->findByPk($id);



…and the _form view file contains the form:




...

<div class="span-10">

    <?php echo $form->textfield($model, 'shopName');?>

    <?php echo $form->textfield($model->shopDetailsRelation, 'description');?>

</div>

...



For those $id that have data in both tables the view renders perfectly fine. However, if a certain $id is not in table ShopDetails the rendering fails.

I have seen that if there is no data $model->shopDetailsRelation is null, doesn’t even model the table (column names, column types, etc…). Perhaps a solution is to instantiate the model object manually in the controller??, like this:




if($model->shopDetailsRelation === null){

    $model->shopDetailsRelation = new ShopDetails();

}



Is that a good approach? Is there a better way to fix it?

Thanks in advance!

Alejandro.

Well, I think you are now standing at a crossroad.

Yii’s Active Record doesn’t support automated creating and/or updating of the related models. It only supports retrieving of the existing related models. :)

Some people, including me, only use Yii’s AR for the creating/updating of the related models.

For example, I do something like this for creation …

  1. Create the main model instance

  2. Create one or more instances of the related model

  3. Pass them to a view form

  4. Receive inputs from the view form to those model instances

  5. Save the main model instance

  6. Set the FK of the related model instance(s)

  7. Save the related model instance(s)

There’s no magic.

Other people think it’s cumbersome and boring, and use extensions to automate the creating/updating of the related models.

Please search the extensions section of this site.

http://www.yiiframework.com/extensions/

Also yiiext of github might be a good place.

There should be many of them …

I see… Thanks for replying.

FYI, take a look at this thread :)

http://www.yiiframework.com/forum/index.php/topic/32511-stable-solution-for-saving-relational-active-record

I think this is what you need for saving data into relational tables:

http://yiiext.github.com/extensions/with-related-behavior/index.html

try it and if you have problems deploying it let us know.

As for displaying multimodel form I say you follow softark instructions. In your controller instantiate both models and pass them to the view. In the view render each form from the respective model [I have not tested this solution so I expect your feedback]

Thank you for this. It seems useful, but doesn’t apply to the specific models I’m working on right now. But I’ll keep it in mind for future developments.

That’s what I’ve done and it works.