Sunil kumar
Sunil kumar

Reputation: 771

Which option is best to update in CakePHP2 With MySql

$this->Model->id = $id;
$this->Model->saveField('field', 'value');

OR

$this->Model->set(array('fieldname' => 'value'));
$this->Model->save();

OR

$this->Model->updateAll(array("fieldName"=>"value"),array("fieldName"=>"condition"));

OR

$this->Model->save($this->request->data);

There are many option in cakephp to update data which also create confusion to choose one.

Thanks in advance..

Upvotes: 0

Views: 100

Answers (1)

Poorvi Gandhi
Poorvi Gandhi

Reputation: 189

All have their own usage.please find my answers along with ur code.

  1. When you wish to update one column for given id. you can use below code. $this->Model->id = $id; $this->Model->saveField('field', 'value');

  2. It can be update multiple columns for given id. Here you added one field only. so, it will update only one column [affected to one row only]. $this->Model->set(array('fieldname' => 'value')); $this->Model->save();

  3. It will update field for multiple rows on which condition is satisfied. $this->Model->updateAll(array("fieldName"=>"value"),array("fieldName"=>"condition"));

  4. Below line mainly used with insert statement, where all columns need to be saved. it may also used for update row too.

$this->Model->save($this->request->data);

I hope you find your answer.

Upvotes: 1

Related Questions