Dear All,
I'm still having trouble with self referential HABTM relationships but
I'm almost there. I have quite a neat setup now using an extended
associations behaviour (http://bakery.cakephp.org/articles/view/add-
delete-habtm-behavior) but it's still doing funny things.
Essentially, I have a load of users who can be friends with each
other. When you ask someone to be your friend they instantly get added
as a friend and they can confirm or deny. To facilitate this
relationship I have a join table:
users_users
id | user_id | friend_id | accepted
The extra field "accepted" keeps track of which requests are pending
(0), accepted (1) or denied (2).
Everything works fine the first time you add a friend but when you
start to add more friends, every "accepted" value for every entry
where "user_id" matches the user gets set to 0. It's all very strange.
I'm not sure if anyone will be able to help but thought it worth
posting.
Thanks in advance, Dave.
Users model...
<code>
<?php
class User extends AppModel {
var $name = 'User';
var $actsAs = 'ExtendAssociations';
var $hasAndBelongsToMany = array(
'friend' => array('className' => 'User',
'joinTable' =>
'users_users',
'foreignKey' =>
'user_id',
'associationForeignKey' => 'friend_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'admirer' => array('className' => 'User',
'joinTable' =>
'users_users',
'foreignKey' =>
'friend_id',
'associationForeignKey' => 'user_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
?>
</code>
Users controller...
<code>
.....
function friend(){
$temp = $this->Session->read();
$userid = $temp['Auth']['User']['id'];
if($this->params['named']['type']=='reply'){
//update friend request...
$this->User->habtmUpdate('admirer', $userid,
$this->params['named']
['friendid'], array('accepted'=>$this->params['named']['status']));
if($this->params['named']['status']==1){
//reciprocate friendship...
$this->User->habtmAdd('friend', $userid,
$this->params['named']
['friendid'], array('accepted'=>1));
$this->Session->setFlash(__('Friendship
reciprocated', true));
}else{
$this->Session->setFlash(__('Freindship
ignored', true));
}
$this->redirect(array('controller'=>'users','action'=>'view',
$userid));
}else{
//create new friendship...
$this->User->habtmAdd('friend', $userid,
$this->params['named']
['friendid'], array('accepted'=>$this->params['named']['status']));
$this->Session->setFlash(__('User has been added as a
friend',
true));
$this->redirect(array('controller'=>'users','action'=>'view',$this-
>params['named']['friendid']));
}
}
.....
</code>
URL to add a new friend...
<code>
http://localhost/project/users/friend/friendid:6/status:0/type:new
</code>
URL to accept a friend request...
<code>
http://localhost/project/users/friend/friendid:6/status:1/type:reply
</code>
URL to refuse a friend request...
<code>
http://localhost/project/users/friend/friendid:6/status:2/type:reply
</code>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---