SaveAll for CakePHP (part 3)
Recently i needed to save multiple entries for the same model and i thought saveAll should do the trick.
After trying some different approaches i found the right one. This approach can be used for both new entries and to update entries.
Controller
(/app/controller/tasks_controller.php)
New entry:
function add() {
if (!empty($this->data)) {
$this->Task->create();
if ($this->Task->saveAll($this->data)) {
$this->Session->setFlash('Tasks saved');
$this->redirect(array('action'=>'index'), null, true);
} else {
$this->Session->setFlash('Tasks could not be saved');
$this->redirect(array('action'=>'index'), null, true);
}
}
}
Edit entry:
function edit($todo_id=null){
if (!empty($this->data)) {
if ($this->Task->saveAll($this->data['Task'])) {
$this->Session->setFlash('Tasks saved');
$this->redirect(array('action'=>'index'), null, true);
} else {
$this->Session->setFlash('Tasks could not be saved');
$this->redirect(array('action'=>'index'), null, true);
}
} else {
// Find ten tasks to edit
$tasks = $this->Task->findAll(null, null, null, 10);
$this->set('tasks', $tasks);
}
}
View
For a new entry:
<?php echo $form->create('Task');?>
<fieldset>
<legend><?php __('New Tasks');?></legend>
<?php
// Lets generate 10 task input fields
for(i=0;i<10;i++){
echo $form->input($i.'.name');
}
?>
</fieldset>
<?php echo $form->end('Submit');?>
To edit entries:
<?php echo $form->create('Task', array('url'=>array('action'=>'edit')));?>
<fieldset>
<legend><?php __('Edit Tasks');?></legend>
<?php
// Loop trough the ten tasks and create form fields. We need at least the ID to update a task.
$count = 0;
foreach($tasks as $task){
echo $form->input($count.'.id', array('value'=>$task['Task']['id']));
echo $form->input($count.'.name', array('value'=>$task['Task']['name']));
$count++;
}
?>
</fieldset>
<?php echo $form->end('Submit');?>
How it works
It’s not much different from the previous saveAll parts, the only really big change is that the form field names are a bit different and you need to specify the right model/array to save in the controller ( the $this->Task->saveAll($this->data['Task']); part).


6 Comments
comments rss [?] | trackback uri [?]I was working with this issue just the other day. Trying to figure out what format saveAll required the resultset array to be in was the difficult part. After reading *very* carefully over the API, I guessed that multiple records of a single model needed to be sent an array with indexed fields (therefore $this->data['ModelName'] in the save), whereas multiple records saved at the same time would use the associative array format.
I guess I should start blogging about this stuff too. ;) Thanks, Robert!
P.S. - $this->cleanUpFields() is deprecated. I believe saveAll calls the replacement method (Model::deconstruct) for this automatically.
“…whereas multiple models saved at the same time…” Models, not records… Typo.
@BrendonKoz
Thanks for the comment, didn’t know $this->cleanUpFields() was depricated (i run an older version, should update :)). Will fix it in the code above!
Thanks for your blog. Saved me lots of time on a project I am working on. I just have one question… How do you stop cakephp from saving blank records. For example, lets say I didn’t fill out all of the task text fields. It has been my experience that they will get saved as blank records. Is there an easy way of stopping this?
[...] links >> setflash CakePHP, Captcha and User Registration Saved by TheHill88 on Wed 03-12-2008 SaveAll for CakePHP (part 3) Saved by wrjih on Mon 17-11-2008 CakephpでsetFlashに入れた値の取り出し方 Saved by [...]
Perfect!
I was trying to do $this->Task->saveAll($this->data) and was not working
Submit your comment
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>