HasMany "through" relationships and multiselect forms
I am running cakephp 2.0.2 and I have the following relationship
Category hasMany CategoryWork Work hasMany CategoryWork CategoryWork belongs to Category,Work
Classic hasMany through relationships for documentation on cakes.
I have an add / edit work form with a select box that allows the user to select multiple categories. The problem I am facing is when a user is editing an existing work, the related categories are not preselected.
I have the following code to populate a select box that actually works:
$categoryWorks = $this->CategoryWork->Category->listCategories(); $this->set(compact('categoryWorks'));
Then my input field looks like this:
$this->Form->input('CategoryWork', array('multiple' => true, 'label' => 'Categories')); =
This will populate the select box, but it will not preselect it using work-related categories.
So I can fix this. I'll just put the selected option for the incoming call and I'll get my preselected categories. But I expected cakephp to be more magical for me :)
My theory is that the cake looks at the ID of the CategoryWork rows returned from the working query and does not match the IDs of the categories populated in the dropdown. It should look at the category_id.
Thoughts on this? I feel like I'm almost there, I just missed something to help the cake do its thing.
Thanks in advance!
source to share