Re: Problem with updating data with HABTM (Posts and Tags).....too much data is deleted from JOIN table.
Anybody knows if there is any chance to have it fixed soon? It is hard to test (or use) RC3 without knowing what should be the correct way of using join tables. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Problem with updating data with HABTM (Posts and Tags).....too much data is deleted from JOIN table.
There is a ticket for this already. I found it yesterday when I was looking into this myself. The problem is new to rc3. Rc2 works just as Cake always has. Add your tests and thoughts there if you can. https://trac.cakephp.org/ticket/5579 About requiring join tables and their pk requirements. Cake has tests for both types of tables. There is also a test for a join with "extra" fields. I personally prefer using a composite pk: CREATE TABLE `modules_users` ( `module_id` int(11) unsigned NOT NULL default '0', `user_id` int(11) unsigned NOT NULL default '0', PRIMARY KEY (`module_id`,`user_id`) ) Cake, on the other hand, creates a pk-less table where the two ids are instead made a unique key. The working result is afaik the same. /Martin --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Problem with updating data with HABTM (Posts and Tags).....too much data is deleted from JOIN table.
I still may add code to the testcase that would show the problem when using a join table that does NOT have a primarykey id field. If someone from core dev could confirm weather or not HABTM join table's must have an id field or if the framework should be made to work with HABTM join tables that do not have an id field this would help. In either case, I think if cake framework should require an id field for the HABTM join tables we should add a warning or log entry that shows the problem during debug >= 2 if a join table is missing the id field. If we would like to support HABTM join tables without a primary key id, I'd be more than happy to add the testcases, I've built as well as the fix that makes it possible. Thanks in advanced LunarDraco(mdcatc) On Oct 15, 3:10 pm, LunarDraco <[EMAIL PROTECTED]> wrote: > An update to this issue, > > After spending some time building testcases I've found that the > correct fix is to add an 'id' to the mapping table. > So in your case Posts_Tags needs an autoincrement 'id' int(10) field. > > 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', > >
Re: Problem with updating data with HABTM (Posts and Tags).....too much data is deleted from JOIN table.
An update to this issue, After spending some time building testcases I've found that the correct fix is to add an 'id' to the mapping table. So in your case Posts_Tags needs an autoincrement 'id' int(10) field. 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 cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-p
Re: Problem with updating data with HABTM (Posts and Tags).....too much data is deleted from JOIN table.
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'
Problem with updating data with HABTM (Posts and Tags).....too much data is deleted from JOIN table.
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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---