I want a function to alter one field, "is_featured" to 1(true) of Event model of given ID, to mark an event as "Featured".
class EventsController extends AppController{ function feature($id){} }
To update a record in database, we first need to get hold of a table using TableRegistry class. We can fetch the instance out of registry using the get() method. The get() method will take the name of the database table as an argument. Now, this new instance is used to get particular record that we want to update.
You can retrieve post data as Array. $post_data= $this->request->data; You can retrieve post data for particular key.
To view records of database, we first need to get hold of a table using the TableRegistry class. We can fetch the instance out of registry using get() method. The get() method will take the name of the database table as argument. Now, this new instance is used to find records from database using find() method.
you can use saveField to do this for example
$this->Event->id = $id; $this->Event->saveField('is_featured', true);
You can perform update in one field by two ways:
$this->Model->id=$id; $this->Model->saveField("fieldName","value");
OR
$this->Model->updateAll(array("fieldName"=>"value"),array("fieldName"=>"condition"));
in second example, first array will update the value in defined field and second array defines the "WHERE" condition
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With