rockfeather
rockfeather

Reputation: 21

Update query in cakephp with more than one condition

I have a query like:

Update `pools` set status='2' where `pid`='1' and `uid`='2'

How do I convert this query in cakephp? i.e. I want to pass that AND condition in query which should update row containing pid='1' and uid='2'.

Upvotes: 2

Views: 22588

Answers (2)

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

UPDATEALL is all you need.

$this->Pool->updateAll(array('status'=>2), array('Pool.pid'=>1,'Pool.uid' => 2));

For more documentation.

http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-array-conditions

If you already have id for the row which you want to update then no need to use update cakephp is very much clever enough to understand it.

For example.

if you have Pool.id which is row of record then.

$this->data['Pool']['id'] = $pool_id;
$this->data['Pool']['uid'] = $pool_uid;
$this->data['Pool']['other_data'] = $pool_otherData;
$this->Pool->save($this->data['Pool']);

above will automatically save $pool_id row.

Upvotes: 5

Php Geek
Php Geek

Reputation: 1107

I guess this may work...Just try...

$this->Pool->updateAll(array('status'=>2), array('AND' => array('Pool.pid'=>1,'Pool.uid' => 2)));

Upvotes: 0

Related Questions