web-dev-qa-db-ja.com

Cakephp 3の1つのフィールドのみを更新

私のアプリの一部では、いくつかのtableのフィールドis_activeのみを更新し、多くのフィールドを含める必要があります。このフィールドのみを更新し、他のすべてのフィールドの検証と要件を回避するための最良のアプローチは何ですか?

10
Daniel Faria

また、特定の行のみを更新する場合は、次のようにします。

 $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でのデータの更新 )。

12
Manohar Khadka

これは動作します:

$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

9
Isaac Askew

コールバックがトリガーされないようにするには、updateAll()を使用します

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

ここの例を使用する: 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']);
0
Yasen Zhelev