I think that I worked out better solution for this.
The model relation:
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',
),
);
The controller:
function addfriend($friend_id = null) {
$this->data = $this->User->read(array('id'), $this->obAuth-
>getUserId());
$friends = array();
foreach ($this->data['Friend'] as $friend) {
$friends[] = $friend['id'];
}
$friends[] = $friend_id;
$admirers = array();
foreach ($this->data['Admirer'] as $admirer) {
$admirers[] = $admirer['id'];
}
$admirers[] = $friend_id;
unset($this->data['Friend']);
unset($this->data['Admirer']);
$this->data['Friend']['Friend'] = $friends;
$this->data['Admirer']['Admirer'] = $admirers;
if ($this->User->save($this->data)) {
$this->Session->setFlash('Added to Friends.');
}
}
Usage:
Simply put link /users/addfriend/10 and the user with id==10 will be
added to logged user friends and logged user will be added to user
with id==10.
Works perfect.
On 29 Wrz, 14:37, red <[EMAIL PROTECTED]> wrote:
> On 29 Wrz, 12:23, AD7six <[EMAIL PROTECTED]> wrote:
>
> > ... which is by design, otherwise how could you (easily) delete an
> > association. There are a few posts/threads/bakery articles/etc. around
> > with "working with habtm" in the title, after a quick google I am sure
> > you will find the info you are seeking ;).
>
> I've searched, but without success :(
>
> But I worked out this code:
>
> $this->data = $this->User->read(null,
> $this->obAuth->getUserId());
>
> $friends = array();
> $i = 0;
> foreach ($this->data['Friend'] as $friend) {
> $friends[$i++] = $friend['id'];
> }
> $friends[$i] = $id;
>
> $this->data['Friend']['Friend'] = $friends;
>
> if ($this->User->save($this->data)) {
> $this->Session->setFlash('Added to Friends.');
> }
>
> So that looks to work ok. To make both side relation just need to run
> this code for every friend from $friends array.
> Tricky, but works for me right now.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---