Monday, January 24, 2011

How to update fields in CakePHP

Following ways we could update fields in cakePHP

1. For example, we will be updating "name" and "mail" fields in a "User" Model for id "1". We will construct array like that with set memeber method

$this->User->read(null, 1);
$this->User->set(array(
    'name' => 'New name',
    'mail' => 'update@example.com'
));
$this->User->save();

2. Here is another way

$data = array(
           'User' => array(
                        'id'          =>    1,
            'name' => 'New name',
            'mail' => 'update@example.com'
           )
        );

$this->User->save($data, false, array('name','mail'));

3. This way we could update single field

  $this->User->saveField('name', 'New name');


For more information click here

1 comment: