tags:

views:

25

answers:

3

Hi,

I have HABTM relation between "Event" and "Category" models. But in the bridge table, "categories_events" table, there is an extra field called "is_primary"; meaning and Event is belong to 1 primary category and many secondary categories.

I deployed 1 select box named "primary_event" and checkboxes named "seconday_event[]".

When I submit the form the data comes up like this. $this->data['Event']['primary_event'] -> 4; $this->data['Event']['secondary_event'] -> array(1,3,5);

After that, how do I feed those data into the format that Cake recognize and which Model's save function do I call? Please be advised that "is_primary" field must be set to 1 for 'primary_event' row.

Thanks, Moe

A: 

The way I understand your requirement, you'll need to create and save records on the categories_events model (You don't need to explicitly code the model unless it has special behaviour - CakePHP is wise enough to understand what you're doing and use a default model structure).

The first create and save will be the primary, then loop over the array for the secondaries. Remember to do a my_model->create() on each iteration.

Leo
Oh, Can I create a model for a joining table?
Moe Sweet
You can, but you don't have to. A model of the correct name will be created by Cake on the fly if it doesn't already exist. You just need to refer to it, like: CategoryEvent->save();
Leo
ah, I didn't know it is that much automagic. Thank :)
Moe Sweet
CategoriesEvent is the model name.
Moe Sweet
+1  A: 

As soon as my joining table has anything more than the dual keys (probably event_id and category_id in your case, I create it as a "standalone" model and modify the relationships as follows (again, using your example):

Event -- hasMany --> EventCategory <-- hasMany -- Category
Event <-- belongsTo -- EventCategory -- belongsTo --> Category

There's a little less "magic", but I think the magic begins to break down as soon as you introduce an additional property (i.e. field). This also gives you a little more flexibility for the future. If you've already found a reason to introduce one new property, it seems even more likely that you might find a reason to add more.

Rob Wilkerson
You're right. I think I have to accept less magic.Thanks Rob.
Moe Sweet
A: 

is_primary is a function of Events, not the HABTM table you are trying to associate it to. The HABTM links the Events and Categories, not events to events. The is_primary should be stored on the Events table. Then you need to associate sub events to their primary event. Here is how to do that.

1- Add the is_primary field to the events table and make it a bool.

2- Add a primary_event_id to the events table. (This will only contain a value if the event is a sub-event.)

3- Create a relationship between events and itself in the Event model. (http://book.cakephp.org/view/851/Multiple-relations-to-the-same-model) This will allow self referencing so you can reference sub-events to their primary event.

Once you get the ID of the event you are interested in, you can run queries to find all of the sub-events:

$this->Event->find('all', array('conditions' => array('primary_event_id' => $id)));

This will return a list of all sub-events.

cdburgess
Hi, Sorry to make it confusing. is_primary is indeed NOT event's. It belongs to the associations table. I should have named$this->data['Event']['primary_category'] instead of ['primary_event']. My bad
Moe Sweet
So what is `is_primary`? Is it the Event, the Cateogry, or the Event_Cateogry combination?
cdburgess