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 <[email protected]> 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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.


Reply via email to