On Dec 22, 11:53 am, Mattijs <[email protected]> wrote:
> Hello,
>
> Guided by the book, I've created an association which lets users
> choose friends from amongst the other users. It looks something like
> this:
>
> class User extends Model
> {
>                 var $hasAndBelongsToMany = array
>                 (
>                         'Users' => array
>                         (
>                                 'className' => 'Users',
>                                 'joinTable' => 'users_has_users',
>                                 'foreignKey' => 'user_id',
>                                 'associationForeignKey' => 'user_friend_id'
>                         )
>                 );
>
> }
>
> When executing a find('count') to see if a user has someone as a
> friend the manual says I should do the following:
>
> $this->User->id = $this->Auth->user('id');
> $this->User->Users->find('count', array('conditions'=>array
> ('Users.id'=> $friend_id))));

Users is a hideous name for a model :) - Relationship would be one
alternative.
>
> However, my result is '1' regardless of wether the jointable actually
> joins User to Users.

It will never create a join, although I'm not sure what you're
expecting Users.id is the primary key, you'd want user_id or
user_friend_id you'll undoubtedly find numerous discussions in the
group on this.

> It seems to get completely ignored and just
> return a find('count') on the users.

the relevant model is your users model (again, hideous name for
amodel). and the relationships defined on it are the ones that will
define whether or not a join is created.

>
> All the naming is correct through useTable. I am getting no errors
> regarding naming.
>
> Any clues as to what I am missing?

Be aware you cannot use duplicate aliases in association definitions.
In your case you have for example User habtm Friend(className = User).
By using the same alias to actually mean something else you will
probably overwrite some of your data php side, and risk generating
invalid sql on the db.

Also note that your users model has 2 relationships (befriendor_id,
befriendee_id), right now you've probably defined neither and are
trying to query only one side. e.g. if I define you as my friend -
will I show up for you when you look for your friends? With the above
logic (possibly desirable for the example, probably not for others) I
would not - becuase you didn't initiate the relationship.

hth,

AD
--~--~---------~--~----~------------~-------~--~----~
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]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to