This appears to be a bug, I've been fighting the same problem, and
finally had some time to dig into it.
I originally thought the condition that is generated for the delete
was incorrect in the model.php __saveMulti when $this-
>hasAndBelongsToMany['unique'] is true. However I found that the
condition that is built IS correct.

The problem is in the call to $this->{$join}->deleteAll($conditions);
on or about line 1226 of model.php in the function __saveMulti
By default the deleteAll() function does a cascade delete which means
it is going to delete both sides of the relation table.

I've changed my code to be:
$this->{$join}->deleteAll($conditions,false);

So the cascade doesn't occur, and this fixes the problem.

I'll be adding some test and a ticket as soon as I have them complete.


On Oct 2, 9:57 pm, Action <[EMAIL PROTECTED]> wrote:
> I have a form that displays all tags for a specified Post in a text
> input field (separated by a space). If I add a tag to it and run the
> save operation, everything goes fine. BUT, if I remove a tag and save,
> it not only deletes the association with that Post, but also all
> associations that tag has with other Posts from the join table. If I
> set "unique" to true in the model, I have the opposite problem,
> nothing gets deleted when I call the save operation.
>
> My Controller:
>
>         function edit($id = null)
>         {
>                 if($this->Session->read('login') != 'admin')
>                 {
>                          $this->redirect('/users/login');
>                 }
>                 if(!$id)
>                 {
>                         $this->redirect(array('action' => 'index'));
>                 }
>                 else
>                 {
>                         $this->pageTitle = 'Edit Post';
>                         if(!empty($this->data))
>                         {
>                                 $this->Post->create();
>                                 $this->data['Post']['id'] = $id;
>                                 $tags = explode(' ', 
> trim($this->data['Tag']['name']));
>                                 foreach($tags as $tag)
>                                 {
>                                         $this->Post->Tag->create(array('name' 
> => $tag));
>                                         $current_tag = 
> $this->Tag->findByName($tag);
>                                         if(is_array($current_tag))
>                                         {
>                                                 $this->data['Tag']['Tag'][] = 
> $current_tag['Tag']['id'];
>                                         }
>                                         else
>                                         {
>                                                 $this->Post->Tag->save();
>                                                 $this->data['Tag']['Tag'][] = 
> $this->Post->Tag->id;
>                                         }
>                                 }
>                                 $this->Post->save($this->data);
>                                 $this->Session->setFlash('Your changes have 
> been saved.', $layout
> = 'flash_success');
>                         }
>                 }
>         }
>
> My Models:
>
>         var $hasAndBelongsToMany = array(
>                         'Tag' => array('className' => 'Tag',
>                                                 'joinTable' => 'posts_tags',
>                                                 'foreignKey' => 'post_id',
>                                                 'associationForeignKey' => 
> 'tag_id',
>                                                 'conditions' => '',
>                                                 'fields' => '',
>                                                 'order' => '',
>                                                 'limit' => '',
>                                                 'offset' => '',
>                                                 'unique' => '',
>                                                 'finderQuery' => '',
>                                                 'deleteQuery' => '',
>                                                 'insertQuery' => ''),
>         );
>
>         var $hasAndBelongsToMany = array(
>                         'Post' => array('className' => 'Post',
>                                                 'joinTable' => 'posts_tags',
>                                                 'foreignKey' => 'tag_id',
>                                                 'associationForeignKey' => 
> 'post_id',
>                                                 'conditions' => '',
>                                                 'fields' => '',
>                                                 'order' => 'Post.created 
> DESC',
>                                                 'limit' => '',
>                                                 'offset' => '',
>                                                 'unique' => '',
>                                                 'finderQuery' => '',
>                                                 'deleteQuery' => '',
>                                                 'insertQuery' => ''),
>         );
--~--~---------~--~----~------------~-------~--~----~
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