Yup… I have another many-to-many related question… I feel like such a newbie asking so much, but hey… I guess we all gotta learn, eh?
So here's the thing… I have everything set up nicely, and it's working like a charm (and FAST! Yay!)… But now I need to find out how to best link Locations to Stores…
Here's how I picture it:
Since there's only a limited number of Stores available, I'm going to list them by type, and display them in a CTabView on either the create/update actions pages, or a separate action called addStores. So far so good… Each one should be represented by a checkbox, so the store's presence at the location can be enabled/disabled by checking or unchecking the box… (And saving of course)
But how do I do this best in Yii? I can think of doing it really manually, but then I’d have to bypass all the automation of Yii, and I’m sure there has to be a better way… So I hope someone will be able to help with this.
Are the checkboxes initially unrelated with stores?
There's no automagic way to achieve this, but the following steps may simplify your work:
You could define in your store model an attribute named locationInput which is an array indicating the location IDs that this store is at.
In the view, you use activeCheckBoxList() to generate a checkbox list based on the "locationInput" attribute. The list data comes from a findAll() call of the location table.
In your store model, you will need to override beforeSave() to insert links into StoreLocation table using the data in locationInput.
My goal is to define which stores are at which locations. Initially the stores aren't in any of the locations… I would like to show all stores as checkboxes, and if the checkbox is then checked, and submitted, the store will be attached to that particular location.
Say we have the following stores:
Goodmans Store
Sec-Gen
Delgado Drones
And the locations:
Eden
George Town
Haven
So if I go in to the "addStores" (or whatever I decide to name that function) I would like it to show three checkboxes, one for each store, and the checked/unchecked status of it determines if the store exists at that location.
The relation is shown by the status of the checkbox… So in the former case, say that Eden has Goodmans Store, George Town as Sec-Gen, and Haven has Sec-Gen and Delgado Drones… Here's how the addStores action would look in each case:
Eden
[X] Goodmans Store
[ ] Sec-Gen
[ ] Delgado Drones
George Town
[ ] Goodmans Store
[X] Sec-Gen
[ ] Delgado Drones
Haven
[ ] Goodmans Store
[X] Sec-Gen
[X] Delgado Drones
So basically… List all stores, showing them as checkboxes… If the store is connected to the location, it's checked, otherwise it's unchecked… When the user submits the form, the checkboxes that are checked determine which stores are connected to which locations.