NOTE: There are other questions related to this on SO, but none of them really answered it for me. I'm not worried about the server side as there are a lot of ways to handle saving related models info (and plenty examples of how to do this).
What I need to know is: how do I implement the view code to add many child models while creating the parent model?
It, surely, will be something like seen on this page: http://www.yiiframework.com/doc/guide/1.1/en/form.table. Except the code handles an update of a bunch of models, not the insertion of them. Which is what I need.
In short, I have an event form, where the user can add many appointments (each appointment has a day, start_time and end_time). I want to render the appointment fields inside the event form using Yii helpers so I get validation and other framework benefits.
The user will be able to add multiple appointments for the same event while creating the event.
Here is Complete solution, Enjoy the power of yii!
http://scriptbaker.com/how-to-save-multiple-related-models-in-yii-complete-solution/
Table Structure:
Father Model:
Child Model:
Father Controller:
Create Father Form:
Child form:
views\father\child_form.php
This solution uses esaverelatedbehavior developed by sluderitz for saving related models, you can download it here
http://www.yiiframework.com/extension/esaverelatedbehavior/
The correct syntax to render what an array of models (doesn't matter if they are child models or not) requires the instantiation of the model in question. So you would have to do something like this in your view:
Where PROPERTY is the name of the property you want to render the textbox for.
If you want a single form for both creating and updating the parent model, the only way is to instantiate a mock object just to render the form. In my case, Event has a collection of Appointment, so in my controller action I did:
Then, in the view
I'm going to try this extension to save the related models: https://github.com/yiiext/with-related-behavior
Update
If you want to have the child model inside the parent model array in $_POST, you'll have to overwrite the name attribute. Following the question example...