I need some guidance on how to call views within a controller for models that have a relationship defined with the current controller"s model.
In my case, I have a model names "catalog", which has a HAS_MANY relationship with another model called "items". In the "view" view for my "catalog" model, I would like to display an ajax form to let user add "items" to the current "catalog" and also I want to render the current "catalog" model"s items.
In theory, I should have a distinct 'item' controller who should take care of rendering a single 'item' view and creation form.
The issue is that thanks to the relation defined in my 'catalog' model, I am loading all the catalog's items at the same time as when I load a 'catalog' model. But since I also want to be able to independently create 'items' for the current 'catalog' without updating the whole catalog model, I want to render in my 'catalog' view a 'item' create form. I also need to have an independent 'item' view action so that I can render via AJAX newly created 'item' without rendering the whole 'catalog' again.
I'm thinking that in my 'catalog' 'view' view, I should loop through the $catalog->items and for each do a renderPartial of the 'item' 'view' view.
But I have read that using views from another controller within a given controller is an absolute no no. Same thing for rendering the 'item' create form, which belongs to the 'item' controller, but that I want to render within my 'catalog' 'view' view.
Is it acceptable to make an exception for this case? This is what I'm currently doing, in my 'catalog' 'view' view:
foreach($catalog->items as $item):
$this->renderPartial('/item/_view', array ( 'item' => $item ) , FALSE, FALSE );
Let me know if there is a better design pattern that I should be adhering to? Thanks in advance for your help.
Lothaire
I'm afraid I didn't undestand you completely but why you don't use many instances of
CActiveForm
widget forItem
model?In view file:
And then in
/ajax/item/update/
you can do everything you want, including DAO instead of AR. There's no especial need to keep relations btw models in the controller for any call from this form.