Update only one field on Cakephp 3

时间秒杀一切 提交于 2019-12-05 02:41:24

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= abc@gmail.com; // other fields if necessary
 if($users->save($user)){
   // saved
 } else {
   // something went wrong
 }

See here (Updating data in CakePHP3).

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

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

$table->updateAll(['field' => $newValue], ['id' => $entityId]);

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']);

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));
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!