Update only one field on Cakephp 3
Asked Answered
B

6

12

In some part of my app I need to update only the field is_active of some table with a lot of fields. What is the best approach to update only this field and avoid the validations and requiriments of all other fields?

Bouldin answered 13/5, 2015 at 14:53 Comment(3)
Is that a SQL related question or PHP code (CakePHP) question?Cowles
I'm talking about cakephp 3Bouldin
@YasenZhelev this question is from 2010. probably cakephp 2 or even 1Bouldin
D
15

And if you want to update particular row only , use this:

 $users= TableRegistry::get('Users');
 $user = $users->get($id); // Return article with id = $id (primary_key of row which need to get updated)
 $user->is_active = true;
 // $user->email= [email protected]; // other fields if necessary
 if($users->save($user)){
   // saved
 } else {
   // something went wrong
 }

See here (Updating data in CakePHP3).

Duclos answered 13/11, 2016 at 8:59 Comment(4)
if($user->save($user)) this line should replace with if($users->save($user))Kimbrough
Cake 3 has the dirty feature. So unless you modify the other fields, they will not be updated or sent in the query. So this keeps you from overwriting password columns by accident. However, passwords and similar fields should have accessible set to false anyway.Ferity
I'd also call it as $users->save($user, ['associated' => false]), just to be sureWhisper
Keep in mind this will fire any "beforeSave / afterSave" on the table doing saves. If you're relying on values existing and you're limiting field selection (for optimized queries), this won't work.Boutte
T
13

This will work:

$users = TableRegistry::get('Users');
$query = $users->query();
$query->update()
    ->set(['is_active' => true])
    ->where(['id' => $id])
    ->execute();

http://book.cakephp.org/3.0/en/orm/query-builder.html#updating-data

Taro answered 13/5, 2015 at 19:34 Comment(3)
With this method how do you comfirm it was updated and redirect to the necessary locations?Pe
callbacks are not fired during this, so if you need them, this wont workMideast
true. @Isaac Askew: FYI, if you have cakephp beforesave then it won't be triggered with this query->update.Swish
B
2

When you don't want callbacks to be triggered, just use updateAll()

$table->updateAll(['field' => $newValue], ['id' => $entityId]);
Blather answered 25/1, 2017 at 7:55 Comment(0)
C
1

Using the example here: http://book.cakephp.org/3.0/en/orm/database-basics.html#running-update-statements. Run the code below to update all records in table_name_here table with a new value for is_active column.

use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');
$connection->update('table_name_here', ['is_active' => 'new_value_here']);
Cowles answered 13/5, 2015 at 15:28 Comment(0)
P
0

I faced this issue when upgrading my project from 2.10 to 3.x.

In 2.10 you could update a single field using:

$this->Menus->saveField('css', $menucss);

But since this method was deprecated, we do as below now, considering that callbacks will not be triggered:

$this->Menus->updateAll(['css' => $menucss], ['id' => $menu_id]);
Prisilla answered 22/3, 2022 at 8:59 Comment(0)
C
-4

The other answers don't use internationalization and other models props, callbacks, etc. I think this is because of the query builder, it does not use the models and so their behaviors, therefore you should use:

$this->loadModel('Inputs');
$input = $this->Inputs->find()->where(['`key`' => $this->request->data['id']])->first();
$this->Inputs->patchEntity($input, ['prop' => $this->request->data['prop']]);

if ($this->Inputs->save($input)) {
    die(json_encode(true));
} else {
    die(json_encode(false));
}
Crunode answered 29/5, 2015 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.