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+unsubscribe@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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
No comments:
Post a Comment