Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP - How to update multiple records

Tags:

php

cakephp

How can I update a single field of multiple records in CakePHP?

I retrieve multiple records using $this->Item->find('all') and I need to set different values for each of them and save. I do

$items = $this->Item->find('all', array(
    'fields' => array('Item.id', 'Item.order'),
    'conditions'=> array(
        'Item.project_id =' => $this->request->params['project_id'],
    ),
    'order' => array ('Item.order ASC')
));

foreach($items as $key => $item) {
    $item->saveField('Item.order', rand(1, 10));
}

but it raises an error saying

Fatal error: Call to a member function saveField() on a non-object

What am I doing wrong?

like image 543
David Weng Avatar asked May 04 '11 14:05

David Weng


People also ask

How do I update a record in CakePHP?

CakePHP - Update a Record. To update a record in database we first need to get hold of a table using TableRegistry class. We can fetch the instance out of registry using the get() method. The get() method will take the name of the database table as an argument. Now, this new instance is used to get particular record that we want to update.

How to update a record in a database in Java?

To update a record in database, we first need to get hold of a table using TableRegistry class. We can fetch the instance out of registry using the get () method. The get () method will take the name of the database table as an argument. Now, this new instance is used to get particular record that we want to update.

What is validating data in CakePHP?

The Validating Data chapter has more information on how to use the validation features of CakePHP to ensure your data stays correct and consistent. When creating or merging entities from request data you need to be careful of what you allow your users to change or add in the entities.

Why comment with ID 2 is no longer there in CakePHP?

As you can see, the comment with id 2 is no longer there, as it could not be matched to anything in the $newData array. This happens because CakePHP is reflecting the new state described in the request data. Some additional advantages of this approach is that it reduces the number of operations to be executed when persisting the entity again.


2 Answers

I would say that you should use CakePHP Save Many to improve the performance.

Ex:

$data = array(
    array( 'Item' => array('id' => 2, 'order' => rand(1,5)) ),
    array( 'Item' => array('id' => 3, 'order' => rand(1,5)) ),
);
$Model->saveMany($data, array('deep' => true));
like image 110
hugofcampos Avatar answered Sep 19 '22 10:09

hugofcampos


Update: Please note that this is an old answer meant for CakePHP 1.3. For a modern approach please refer to the answer below.

Try this

foreach($items as $key => $item) {
    $this->Item->id = $item['Item']['id'];
    $this->Item->saveField('order', rand(1, 10));
}
like image 36
Hamid Nazari Avatar answered Sep 19 '22 10:09

Hamid Nazari