AD and jyrgen: thanks for responding.
AD: I really appreciate that you answered each of my questions. Very
helpful! God Bless You!
You clarified things for me. I now see that I don't really need Friend
model. I just need a join table (let's just call it friends). I have
tried it. It works beautifully.
When view a user, I can see both Friend and Admirer. The only problem
is when I edit a user, I can only edit Admirers, the Friends are not
shown on screen even after I added line for inputing Friends.
<?php echo $form->input('Friend/Friend', array('options' =>
$friends, 'multiple' => 'multiple'));?>
<?php echo $form->input('Admirer/Admirer', array('options' =>
$admirers, 'multiple' => 'multiple'));?>
Here $form->input('Admirer/Admirer'... line were generated by Cake. It
did not generate the line for inputing Friend for some reason. Even
after I added the line for inputing Friend, it still does not show on
screen. Strange.
Other than that, your way of put two associations works beautifully. I
did not even know that you can put in two associations.
Again, thanks a lot.
Victor
On May 18, 1:50 am, AD7six <[EMAIL PROTECTED]> wrote:
> 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+...
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---