On Jan 22, 2008 7:13 AM, Eemerge <[EMAIL PROTECTED]> wrote:
>
> Hello,
>
> I have 3 tables:
>
> 1. posts
> 2. categories
> 3. post_2_cat
>
> I am trying to use hasAndBelongsToMany to fetch a post and its
> associated categories (from post_2_cat)
>
> The only problem i encountered so far is with table fields names.
> Initially i had post_id and category_id in the posts and categories
> tables. This way, i dont get anything returned with
> hasAndBelongsToMany.
> If i modify the fields names to 'id' , i get the expected results.
>
> var $hasAndBelongsToMany=array('Category'=>
> array('className' => 'Category',
> 'joinTable' => 'post_2_cat',
> 'foreignKey' => 'post_id',
> 'associationForeignKey'=> 'cat_id',
> 'conditions' => '',
> 'order' => '',
> 'limit' => '',
> 'unique' => true,
> 'finderQuery' => '',
> 'deleteQuery' => '',
> )
>
> Above is the code that does this (resides in models/post.php).
What are the column names in post_2_cat? It should be those names (not
from post and category) in this array.
eg if you have:
post_2_cat.post_id
post_2_cat.category_id
foreignKey => post_id
associationForeignKey => category_id
> My question is, how can i use hasAndBelongToMany but not have to use
> "id" in my tables (for example i want to use: post_id instead of id ,
> in the posts table).
$primaryKey = 'post_id';
> Thanks in advance. Also, any other advices are welcome as im quite new
> to cakephp (which rocks btw :) )
Out of curiosity, what does the query look like when you set DEBUG to
2? I'm wondering how the Inflector handles the '_2_' in the join table
name.
b
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---