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
-~----------~----~----~----~------~----~------~--~---