Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update only one field on Cakephp 3

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?

like image 413
Daniel Faria Avatar asked May 13 '15 14:05

Daniel Faria


3 Answers

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).

like image 138
Manohar Khadka Avatar answered Oct 11 '22 05:10

Manohar Khadka


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

$table->updateAll(['field' => $newValue], ['id' => $entityId]);
like image 39
omid Avatar answered Oct 11 '22 03:10

omid


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

like image 24
Isaac Askew Avatar answered Oct 11 '22 05:10

Isaac Askew