I’m wondering what would be considered best pratice when working with has_many and many_many relationships in the following situation:
Suppose I have a many_many relationship between Post and Category.
In the update dialog for my post model I’d then have a CHtml::activeCheckBoxList where the user can check the categories a post belongs to.
I have the following questions about this.
[list=1]
[*]The activeCheckBoxList obviously needs to know what categories are available. Shall I fetch those in the controller and pass them on to the view using the data array in the CController::render method and then do something like this:
The same obivously applies to has_many relationships.
[*]What should the name of the activeCheckBoxList be? Currently I’ve created a public category_ids attribute in my Post class which I then process in the afterSave() method to populate the join table. Is this the way it’s supposed to be done or is there a better way? Something tells me there is
I’m far from being an expert, but here is my opinion.
To my mind, a view should have a minimum of server code. So, views should have minimum of processing and a maximum of variables. I would choose this way
Well, this way is a very simple way. You need to save the post before linking it to its categories. So, doing it on [font="Courier New"]afterSave[/font] is correct. What kind of options are you waiting for?
I was hoping that there’d by another way to do this which wouldn’t require me to manually populate the category_ids array so that the selected options get checked.