Update query in cakephp with more than one condition

21,604

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.

Share:
21,604
rockfeather
Author by

rockfeather

Updated on January 25, 2020

Comments

  • rockfeather
    rockfeather over 4 years

    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'.

  • rockfeather
    rockfeather over 11 years
    I did try and it worked like a charm! but, if I do modify it a little like this:$this->Poo->updateAll(array('receipt_no'=>'abc123'), array('Pool.id'=>9,'Pool.uid' => 23));It takes 'abc123' as a column name and gives error as unknown column
  • Dipesh Parmar
    Dipesh Parmar over 11 years
    you must need to use Model.field_name like array('Pool.receipt_no'=>'abc123')...because cakephp query create table alias automatically as per model name... try this i m sure this is it.
  • rockfeather
    rockfeather over 11 years
    Yest I did use ('Pool.receipt_no'=>'abc123'), but it takes abc123 as field name
  • Dipesh Parmar
    Dipesh Parmar over 11 years
    may i see database table structure...its not possible to take value parameter as column must be something wrong..
  • rockfeather
    rockfeather over 11 years
    CREATE TABLE IF NOT EXISTS pools ( id bigint(11) NOT NULL AUTO_INCREMENT, pool_id bigint(11) NOT NULL, uid varchar(20) COLLATE utf8_unicode_ci NOT NULL, receipt_no varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  • rockfeather
    rockfeather over 11 years
    No this is also not working same error it takes 'abc123' as field name
  • Php Geek
    Php Geek over 11 years
    whats ur cakephp version ??
  • rockfeather
    rockfeather over 11 years
    Ok Finally this worked:$this->Poolcontributor->updateAll(array('receipt_no'=‌​>"'abc123'"), array('Poolcontributor.pool_id'=>9,'Poolcontributor.user_id' => 23)); What I did is i converted 'abc123' in to "'abc123'". really a weird behaviour of cakephp.Thanks for the help!
  • rockfeather
    rockfeather over 11 years
    Ok Finally this worked:$this->Poolcontributor->updateAll(array('receipt_no'=‌​>"'abc123'"), array('Poolcontributor.pool_id'=>9,'Poolcontributor.user_id' => 23)); What I did is i converted 'abc123' in to "'abc123'". really a weird behaviour of cakephp