Monday, September 29, 2008

Re: Saving self-referential-HABTM relationships...

I imagine it should work if you use different join tables for each HABTM
relationship. Regardless of cakephp this is what I imagine you'd need in
this situation (imagine what would happen if a user is both a friend and
an admirer of another user).

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

'Admirer' => array('className' => 'User',
'joinTable' => 'admirers_users',
'foreignKey' => 'admirer_id',
'associationForeignKey' => 'user_id'
)
);

wallerdm wrote:
> Dear All,
>
> Forgive me if this is covered elsewhere. I'm playing with cakePHP as
> an alternative to Rails and am getting to grips with it by creating a
> really simple social network app.
>
> I've got some nice authentication and user profiles sorted and that's
> all displaying great but now I've come to add the "add as a friend"
> functionality and I'm struggling.
>
> Every user in the "users" table has an "id" and I also have a table
> set up, "users_users", to hold the self referential join. This table
> has a an "id", a "user_id" and a "friend_id".
>
> My user model contains the following...
>
> <?php
> class User extends AppModel {
>
> var $name = 'User';
> var $hasOne = 'Profile';
> var $hasMany = array('Status' => array('order' => 'Status.created
> DESC' ));
>
> var $hasAndBelongsToMany = array(
> 'Friend' => array('className' => 'User',
> 'joinTable' => 'users_users',
> 'foreignKey' => 'user_id',
> 'associationForeignKey' => 'friend_id'
> ),
> 'Admirer' => array('className' => 'User',
> 'joinTable' => 'users_users',
> 'foreignKey' => 'friend_id',
> 'associationForeignKey' => 'user_id'
> )
> );
>
> }
> ?>
>
> And my users_controller contains...
>
> <?php
> class UsersController extends AppController {
>
> .....
> function addfriend($friend_id = null) {
> $user_id = $this->Session->read('User');
> $this->data['User']['id'] = $user_id['id'];
> $this->data['Friend']['id'] = $friend_id;
> print_r($this->data);
> if($this->User->saveAll($this->data)){
> echo 'saved';
> }else{
> echo 'failed';
> }
> }
> .....
>
> }
> ?>
>
> Now I know that I've got the data element wrong for the database save
> because it isn't working and the SQL that's generated when you add a
> friend is incorrect.
>
> If you're logged in as a user with id 1 and want to become friends
> with the user with id 4 (http://localhost/network/users/addfriend/4),
> you get...
>
> data = Array ( [User] => Array ( [id] => 1) [Friend] => Array ( [id]
> => 4 ) )
>
> ...and the SQL from the CakePHP debug panel is...
>
> UPDATE `users` SET `id` = 1 WHERE `users`.`id` = 1
>
> Is anyone able to help me get this whole self-referential-HABTM
> relationship thing sorted?
>
> Thanks in advance.
>
> Dave.
>
> >
>
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: