私のアプリの一部では、いくつかのtable
のフィールドis_active
のみを更新し、多くのフィールドを含める必要があります。このフィールドのみを更新し、他のすべてのフィールドの検証と要件を回避するための最良のアプローチは何ですか?
また、特定の行のみを更新する場合は、次のようにします。
$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
}
ここを参照してください( CakePHP3でのデータの更新 )。
これは動作します:
$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
コールバックがトリガーされないようにするには、updateAll()を使用します
$table->updateAll(['field' => $newValue], ['id' => $entityId]);
ここの例を使用する: http://book.cakephp.org/3.0/en/orm/database-basics.html#running-update-statements 。以下のコードを実行して、table_name_here
テーブルのすべてのレコードをis_active
列の新しい値で更新します。
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');
$connection->update('table_name_here', ['is_active' => 'new_value_here']);