Hi Victor,

Hope I can clarify a couple of things for you.

On 17 mayo, 20:47, Victor <[EMAIL PROTECTED]> wrote:
> I am trying to define the right model and relationship for User and
> Friend. Here are my tables
>
> CREATE TABLE users {
> id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
> name VARCHAR (20)
>
> }
>
> CREATE TABLE friends {
> user_id INT UNSIGNED,
> friend_id INT UNSIGNED
> PRIMARY KEY(user_id, friend_id)
>
> }
>
> The next thing is to define relationship. I use bake command to create
> relationship. User hasAndBelongsToMany Friend.
>
> So my User model includes this:
>
>         var $hasAndBelongsToMany = array(
>                         'Friend' => array('className' => 'Friend',
>                                                 'joinTable' => 'friends',
>                                                 'foreignKey' => 'user_id',
>                                                 'associationForeignKey' => 
> 'friend_id',
>                                                 'conditions' => '',
>                                                 'fields' => '',
>                                                 'order' => '',
>                                                 'limit' => '',
>                                                 'offset' => '',
>                                                 'unique' => '',
>                                                 'finderQuery' => '',
>                                                 'deleteQuery' => '',
>                                                 'insertQuery' => ''),
>         );
>
> The question is that does the Friend model

What friend model, a friend is another user right...? the table named
"friends" is your join table, it is not in itself a model. In the
above if you change 'className' => 'Friend', to 'className' => 'User',
your class definitions would then appear to be correct.

> need to include Friend
> hasAndBelongsMany User relationship? If it does what should it look
> like?
>
> Should Friend model just simply have Friend belongs User relationship?

You definitely should not have a friend model (unless there's
something you aren't saying).

>
> Is my friends table setup right? Is there better way?

The tables look correct, it's just a typical join table definition.

>
> Will this setup not allow cases where user A has B as friend but B
> does not have A as friend?


The way the above association works is 1 way. That is to say: your
User model should have 2 habtm association definitions if you want to
know who is a friend and who has the current user as a friend:

var $hasAndBelongsToMany = array(
                         'Friend' => array('className' => 'User',
                                                 'joinTable' =>
'friends',
                                                 'foreignKey' =>
'user_id',
 
'associationForeignKey' => 'friend_id'),
                         'Admirer' => array('className' => 'User',
                                                 'joinTable' =>
'friends',
                                                 'foreignKey' =>
'friend_id',
 
'associationForeignKey' => 'user_id')
);

This would then allow you to say "User X has Y Friends. Z other users
have X as a Friend". You can use bindModel/unbindModel to add/remove
these relationships on the fly if necessary.

You probably can rebake and get the above settings (and associated
views etc.), and just for clarity: there is no problem at all in
having a self joining association.

hth,

AD
PS Did you look here before starting this thread ;)
http://groups.google.com/group/cake-php/search?group=cake-php&q=self+habtm


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to