RE: How do I circumvent the default HABTM deletion behaviour
Doublecheck your relations, naming can be confusing also Check / read about 'with' => attribute for the HABTM table Not sure about your set up for sorting HABTM. Why would 1 object be sortable by different people? Example User hasMany Gallery hasMany Image User could sort Images in their Galleries Also Category SubCategory is a bad way to start things off. Everything is a Catergory just give it a Category.parent_id. Everything in 1 table. Endless options there. You not going to 1 day need a a SubSubCatergory or SuperSubCatergory. Good luck -Original Message- From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf Of adam_g2000 Sent: Tuesday, March 01, 2011 9:05 PM To: CakePHP Subject: Re: How do I circumvent the default HABTM deletion behaviour Hi Krissy, Thanks for your response, I think my question might have been badly worded. The join table has nothing more in it than the two id fields. I guess I don't want to circumvent cakePHP, I'm happy to let it do its thing but I want it to rewrite the information it erases. It wipes all, but doesn't save all. I could write more code that reads the join table first, then rewrites it but Cake seems to be more clever than that, shouldn't it be doing that for me? Thanks, Adam. On Mar 2, 11:19 am, "Krissy Masters" wrote: > My guess, and only a guess. I have not looked at the code under the hood. > > But: Say you have 15 records for 1 User saved. Now User goes back to change > something say change 1 record -> save How does the db know which record your > altering? You would need to grab all current records already saved and > compare the values of each record your now saving against whats saved > already and then save -> update the 1 record. Might be easy enough if you > have standard 2 fields in HABTM but if you had extra fields in HABTM you > would need to compare every field => value for every record for that user > has. > > I would say in the end it's just "easier" to wipe all and save all. Because > in order to save just the changes each time your going to have to: > compare old data vs new data and only save the differences looking not only > for changes to existing records but also additional new records or records > being deleted. > > I tried doing what you asking and in the end after a few attempts it was > just more work so let Cake do its thing. > > K > > > > -Original Message- > From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf > > Of adam_g2000 > Sent: Tuesday, March 01, 2011 5:18 PM > To: CakePHP > Subject: How do I circumvent the default HABTM deletion behaviour > > I had a problem with updating data in a loop, during the update, the > assoc. HABTM data is wiped in the join table. I was led back to the > Cookbook where I found this is default behaviour (last section, this > pagehttp://book.cakephp.org/view/1034/Saving-Related-Model-Data-HABTM). > > What isn't clear (or I can't figure out) is how to prevent this > default behaviour. I have this method in my controller: > > function up($id = null, $order = null) { > if (!$id or !$order) { > $this->Session->setFlash(__('Invalid id or order > number', true)); > $this->redirect(array('action'=>'index')); > } > // Create an array of the image table contents of IDs and > Orders. > $conditions = array( > //'fields' => > array('id','order'), > 'order' => > 'Subcategory.order ASC' > ); > $subcategories = $this->Subcategory->find('all', > $conditions); > // Set the direction for the image to travel (up) > $delta = -1; > // index = the array item you want to move > // delta = the direction and number of spaces to move the > item. > $index = $order -1; > $index2 = $index + $delta; > // Move the elements in the array > $temp_item = > $subcategories[$index2]['Subcategory']['order']; > $subcategories[$index2]['Subcategory']['order'] = > $subcategories[$index]['Subcategory']['order']; > $subcategories[$index]['Subcategory']['order'] = $temp_item; > // Write it all back to the database > foreach ($subcategories as $subcategory): >
Re: How do I circumvent the default HABTM deletion behaviour
Hi Krissy, Thanks for your response, I think my question might have been badly worded. The join table has nothing more in it than the two id fields. I guess I don't want to circumvent cakePHP, I'm happy to let it do its thing but I want it to rewrite the information it erases. It wipes all, but doesn't save all. I could write more code that reads the join table first, then rewrites it but Cake seems to be more clever than that, shouldn't it be doing that for me? Thanks, Adam. On Mar 2, 11:19 am, "Krissy Masters" wrote: > My guess, and only a guess. I have not looked at the code under the hood. > > But: Say you have 15 records for 1 User saved. Now User goes back to change > something say change 1 record -> save How does the db know which record your > altering? You would need to grab all current records already saved and > compare the values of each record your now saving against whats saved > already and then save -> update the 1 record. Might be easy enough if you > have standard 2 fields in HABTM but if you had extra fields in HABTM you > would need to compare every field => value for every record for that user > has. > > I would say in the end it's just "easier" to wipe all and save all. Because > in order to save just the changes each time your going to have to: > compare old data vs new data and only save the differences looking not only > for changes to existing records but also additional new records or records > being deleted. > > I tried doing what you asking and in the end after a few attempts it was > just more work so let Cake do its thing. > > K > > > > -Original Message- > From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf > > Of adam_g2000 > Sent: Tuesday, March 01, 2011 5:18 PM > To: CakePHP > Subject: How do I circumvent the default HABTM deletion behaviour > > I had a problem with updating data in a loop, during the update, the > assoc. HABTM data is wiped in the join table. I was led back to the > Cookbook where I found this is default behaviour (last section, this > pagehttp://book.cakephp.org/view/1034/Saving-Related-Model-Data-HABTM). > > What isn't clear (or I can't figure out) is how to prevent this > default behaviour. I have this method in my controller: > > function up($id = null, $order = null) { > if (!$id or !$order) { > $this->Session->setFlash(__('Invalid id or order > number', true)); > $this->redirect(array('action'=>'index')); > } > // Create an array of the image table contents of IDs and > Orders. > $conditions = array( > //'fields' => > array('id','order'), > 'order' => > 'Subcategory.order ASC' > ); > $subcategories = $this->Subcategory->find('all', > $conditions); > // Set the direction for the image to travel (up) > $delta = -1; > // index = the array item you want to move > // delta = the direction and number of spaces to move the > item. > $index = $order -1; > $index2 = $index + $delta; > // Move the elements in the array > $temp_item = > $subcategories[$index2]['Subcategory']['order']; > $subcategories[$index2]['Subcategory']['order'] = > $subcategories[$index]['Subcategory']['order']; > $subcategories[$index]['Subcategory']['order'] = $temp_item; > // Write it all back to the database > foreach ($subcategories as $subcategory): > $this->Subcategory->create(false); > $this->Subcategory->save($subcategory); > endforeach; > $this->redirect(array('action' => 'index')); > } > > You can see I've commented out fields in the conditions in the vain > hope that all the data would be updated. However the behaviour is the > same, gone is the contents of my join table. > > I've been shown SortableBehaviour, which I expect will eventually > replace my less esteemed code (I'm a newbie), but I want to fill the > gap in my knowledge and have this work first. > > Any assistance would be gratefully received. > > Adam. > > -- > Our newest site for th
RE: How do I circumvent the default HABTM deletion behaviour
My guess, and only a guess. I have not looked at the code under the hood. But: Say you have 15 records for 1 User saved. Now User goes back to change something say change 1 record -> save How does the db know which record your altering? You would need to grab all current records already saved and compare the values of each record your now saving against whats saved already and then save -> update the 1 record. Might be easy enough if you have standard 2 fields in HABTM but if you had extra fields in HABTM you would need to compare every field => value for every record for that user has. I would say in the end it's just "easier" to wipe all and save all. Because in order to save just the changes each time your going to have to: compare old data vs new data and only save the differences looking not only for changes to existing records but also additional new records or records being deleted. I tried doing what you asking and in the end after a few attempts it was just more work so let Cake do its thing. K -Original Message- From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf Of adam_g2000 Sent: Tuesday, March 01, 2011 5:18 PM To: CakePHP Subject: How do I circumvent the default HABTM deletion behaviour I had a problem with updating data in a loop, during the update, the assoc. HABTM data is wiped in the join table. I was led back to the Cookbook where I found this is default behaviour (last section, this page http://book.cakephp.org/view/1034/Saving-Related-Model-Data-HABTM). What isn't clear (or I can't figure out) is how to prevent this default behaviour. I have this method in my controller: function up($id = null, $order = null) { if (!$id or !$order) { $this->Session->setFlash(__('Invalid id or order number', true)); $this->redirect(array('action'=>'index')); } // Create an array of the image table contents of IDs and Orders. $conditions = array( //'fields' => array('id','order'), 'order' => 'Subcategory.order ASC' ); $subcategories = $this->Subcategory->find('all', $conditions); // Set the direction for the image to travel (up) $delta = -1; // index = the array item you want to move // delta = the direction and number of spaces to move the item. $index = $order -1; $index2 = $index + $delta; // Move the elements in the array $temp_item = $subcategories[$index2]['Subcategory']['order']; $subcategories[$index2]['Subcategory']['order'] = $subcategories[$index]['Subcategory']['order']; $subcategories[$index]['Subcategory']['order'] = $temp_item; // Write it all back to the database foreach ($subcategories as $subcategory): $this->Subcategory->create(false); $this->Subcategory->save($subcategory); endforeach; $this->redirect(array('action' => 'index')); } You can see I've commented out fields in the conditions in the vain hope that all the data would be updated. However the behaviour is the same, gone is the contents of my join table. I've been shown SortableBehaviour, which I expect will eventually replace my less esteemed code (I'm a newbie), but I want to fill the gap in my knowledge and have this work first. Any assistance would be gratefully received. Adam. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
How do I circumvent the default HABTM deletion behaviour
I had a problem with updating data in a loop, during the update, the assoc. HABTM data is wiped in the join table. I was led back to the Cookbook where I found this is default behaviour (last section, this page http://book.cakephp.org/view/1034/Saving-Related-Model-Data-HABTM). What isn't clear (or I can't figure out) is how to prevent this default behaviour. I have this method in my controller: function up($id = null, $order = null) { if (!$id or !$order) { $this->Session->setFlash(__('Invalid id or order number', true)); $this->redirect(array('action'=>'index')); } // Create an array of the image table contents of IDs and Orders. $conditions = array( //'fields' => array('id','order'), 'order' => 'Subcategory.order ASC' ); $subcategories = $this->Subcategory->find('all', $conditions); // Set the direction for the image to travel (up) $delta = -1; // index = the array item you want to move // delta = the direction and number of spaces to move the item. $index = $order -1; $index2 = $index + $delta; // Move the elements in the array $temp_item = $subcategories[$index2]['Subcategory']['order']; $subcategories[$index2]['Subcategory']['order'] = $subcategories[$index]['Subcategory']['order']; $subcategories[$index]['Subcategory']['order'] = $temp_item; // Write it all back to the database foreach ($subcategories as $subcategory): $this->Subcategory->create(false); $this->Subcategory->save($subcategory); endforeach; $this->redirect(array('action' => 'index')); } You can see I've commented out fields in the conditions in the vain hope that all the data would be updated. However the behaviour is the same, gone is the contents of my join table. I've been shown SortableBehaviour, which I expect will eventually replace my less esteemed code (I'm a newbie), but I want to fill the gap in my knowledge and have this work first. Any assistance would be gratefully received. Adam. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php