Monday, September 3, 2012

Re: Can't get HABTM right

Thanks for the quick reply!

I renamed the table to what you said and put the $habtm in the Format model. So Format and Accessory model both have $habtm to each other now. 

But I still can't get the right accessories to show. What should I put in the AccessoriesController? I tried that contain part you said, but can't get it to work... I don't need them in the FormatController, that part doesn't need to show any accessories. In the AccessoriesController (so that if you go to mywebsite.com/accessories) I need to get the accessories that belong to a certain Format. I hope you can help me again! :)

Op maandag 3 september 2012 10:18:41 UTC+2 schreef Jeremy Burns het volgende:
A few things to try...

The joining table should be accessories_formats (the tables appear in alphabetical order).

Remove the $hasMany declaration from the Format model - it should be a $hasAndBelongsToMany association as well.

You have referenced 'FormatAccessorie' - that is neither the singular or plural version of Accessory.

To get the accessories that belong to a Format, simply comntain the Accessory model (and vice versa) and the HABTM association will bring them in.


On 3 Sep 2012, at 09:04, iFemke <femkev...@gmail.com> wrote:

Hi,

I can't get a HABTM relationship right, I can't get it to show me the results I need. I looked at Cake's documentation and Googled, and I tried differents things, rebuilding it from examples I've found online. I just don't know what I'm doing wrong.

The situation is as following. I have Formats (stored in db table formats) and Accessories (stored in db table accessories). Accessories belongs to one or more Formats. So one accessory can belong to many formats and one format can have different accessories. I made a linking table named formats_accessories (with id, format_id and accessory_id). Then I let cake bake do it's magic. 

My Accessory model:
public $hasAndBelongsToMany = array(
'Format' => array(
'className' => 'Format',
'joinTable' => 'formats_accessories',
'foreignKey' => 'accessory_id',
'associationForeignKey' => 'format_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
);

My Format model:
public $hasMany = array(
'FormatsAccessorie' => array(
'className' => 'FormatsAccessorie',
'foreignKey' => 'format_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);

I don't think this is right, but whatever I tried, changing 'FormatsAccessorie' to Format or changing the $hasMany to $hasManyAndBelongsTo, it just won't work!

What I want to accomplish. In the index function of AccessoriesController I need to show the accessories that belong to a certain format_id. So not all accessories (I know how to make that), but just the accessories that belong to a certain format (which id i set in a session). If you go to mywebsite.com/accessories it needs to show the accessories that belongs to a format you already picked (and is stored in a session).

I really hope you can help me. I know google and this group are full of HABTM questions, but I already looked at so many that I've given up because I just can't get it right.

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake...@googlegroups.com.
To unsubscribe from this group, send email to cake-php+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 

No comments: