在 Cakephp 3 上只更新一个字段
Update only one field on Cakephp 3
在我的应用程序的某些部分,我只需要更新一些 table
的 is_active
字段,其中有很多字段。仅更新此字段并避免所有其他字段的验证和要求的最佳方法是什么?
使用此处的示例:http://book.cakephp.org/3.0/en/orm/database-basics.html#running-update-statements。 运行 下面的代码用 is_active
列的新值更新 table_name_here
table 中的所有记录。
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');
$connection->update('table_name_here', ['is_active' => 'new_value_here']);
这会起作用:
$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
其他答案不使用国际化和其他模型道具、回调等。
我认为这是因为查询构建器,它不使用模型等它们的行为,因此您应该使用:
$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));
}
如果您只想更新特定行,请使用:
$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
}
看这里 (Updating data in CakePHP3)。
当你不想触发回调时,只需要使用updateAll()
$table->updateAll(['field' => $newValue], ['id' => $entityId]);
我在将我的项目从 2.10 升级到 3.x 时遇到了这个问题。
在 2.10 中,您可以使用以下方式更新单个字段:
$this->Menus->saveField('css', $menucss);
但由于该方法已被弃用,考虑到不会触发回调,我们现在做如下:
$this->Menus->updateAll(['css' => $menucss], ['id' => $menu_id]);
在我的应用程序的某些部分,我只需要更新一些 table
的 is_active
字段,其中有很多字段。仅更新此字段并避免所有其他字段的验证和要求的最佳方法是什么?
使用此处的示例:http://book.cakephp.org/3.0/en/orm/database-basics.html#running-update-statements。 运行 下面的代码用 is_active
列的新值更新 table_name_here
table 中的所有记录。
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');
$connection->update('table_name_here', ['is_active' => 'new_value_here']);
这会起作用:
$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
其他答案不使用国际化和其他模型道具、回调等。 我认为这是因为查询构建器,它不使用模型等它们的行为,因此您应该使用:
$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));
}
如果您只想更新特定行,请使用:
$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
}
看这里 (Updating data in CakePHP3)。
当你不想触发回调时,只需要使用updateAll()
$table->updateAll(['field' => $newValue], ['id' => $entityId]);
我在将我的项目从 2.10 升级到 3.x 时遇到了这个问题。
在 2.10 中,您可以使用以下方式更新单个字段:
$this->Menus->saveField('css', $menucss);
但由于该方法已被弃用,考虑到不会触发回调,我们现在做如下:
$this->Menus->updateAll(['css' => $menucss], ['id' => $menu_id]);