Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update 1 field in CakePHP?

Tags:

cakephp

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){} } 
like image 824
Moe Sweet Avatar asked Sep 01 '10 07:09

Moe Sweet


People also ask

How to update data in CakePHP?

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.

How can I get post data in CakePHP?

You can retrieve post data as Array. $post_data= $this->request->data; You can retrieve post data for particular key.

How can I get data from database in CakePHP?

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.


Video Answer


2 Answers

you can use saveField to do this for example

$this->Event->id = $id; $this->Event->saveField('is_featured', true); 
like image 190
dqminh Avatar answered Oct 01 '22 15:10

dqminh


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

like image 36
Vineet Kumar Avatar answered Oct 01 '22 15:10

Vineet Kumar