how to update a single attribute of an item in a table using CakePHP -


i'm trying update 1 piece of information user saved in database, , cakephp accepts array of values each attribute, don't have values other attributes , cakephp not update row because expecting values found in row.

first way : create new query object using query():

       $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


second way: using patchentity


using fieldlist() option when creating or merging data entity:

this allow update or insert field u want

         $user = $this->users->get($id);          if ($this->request->is(['post', 'put'])) {              //assume $this->request->data contains field ['name' => 'myname', 'title' => 'hacked!'];              $user = $this->users->patchentity($user, $this->request->data, [                   'fieldlist' => ['title']]);              // allow title changed              if ($this->users->save($user)) {                 $this->flash->success(__('your user has been updated.'));                 return $this->redirect(['action' => 'index']);             }             $this->flash->error(__('unable update user.'));         } 

http://book.cakephp.org/3.0/en/orm/saving-data.html#changing-accessible-fields

http://book.cakephp.org/3.0/en/orm/query-builder.html#updating-data


Comments