Skip to content

Instantly share code, notes, and snippets.

@mmhan
Created November 24, 2011 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmhan/1392421 to your computer and use it in GitHub Desktop.
Save mmhan/1392421 to your computer and use it in GitHub Desktop.
Editing multiple records with saveAll()
function edit() {
if(!empty($this->data)) {
$this->Profile->saveAll($this->data['Profile']);
}
else {
$this->data['Profile'] = Set::combine($this->Profile->find('all'), '{n}.Profile.id', '{n}.Profile');
}
}
<?php
/**
* Converting findAll() to saveAll() for editing multiple records.
* http://nuts-and-bolts-of-cakephp.com/2008/10/27/editing-multiple-records-with-saveall/
**/
/*****
* findAll() format
Array
(
[0] => Array
(
[Profile] => Array
(
[id] => 21
[name] => bob 3
[created] => 2008-10-27 13:01:30
)
)
[1] => Array
(
[Profile] => Array
(
[id] => 20
[name] => larry 5
[created] => 2008-10-27 13:01:30
)
)
)
**/
/***
* saveAll() format
[Profile] => Array
(
[21] => Array
(
[id] => 21
[name] => bob 3
[created] => 2008-10-27 13:01:30
)
[20] => Array
(
[id] => 20
[name] => larry 5
[created] => 2008-10-27 13:01:30
)
);
**/
?>
echo $form->create('Profile', array('action'=>'edit'));
foreach($this->data['Profile'] as $key => $value) {
echo $form->input('Profile.'.$key.'.name');
echo $form->input('Profile.'.$key.'.id');
}
echo $form->end('Save All Profiles');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment