CakePHP updateAll() issues

后端 未结 2 910
傲寒
傲寒 2021-01-02 04:41

I have an images table with a column called type. I simply want to update all the rows to change the type to gallery where the user_id

相关标签:
2条回答
  • 2021-01-02 05:02

    Found this on the manual:

    The $fields array accepts SQL expressions. Literal values should be quoted manually.

    Thus, the following should work:

    $this->Image->updateAll(
        array('Image.type' => "'gallery'"), 
        array('Image.user_id' => $this->Auth->user('id'))
    );
    
    0 讨论(0)
  • 2021-01-02 05:22

    In your model do something like this in your method ....

    public function saveImage($type='')
    {
     // I would add a test for $type
     $db = $this->getDataSource();
     $fields = array('type' => $db->value($type, 'string')); // $db->value() will format strings needed for updateAll()
     $condition = array('user_id' => $this->Auth->user('id'));
    
     // I would add a test for user id before running updateAll()
    
      $this->updateAll($fields, $conditions);
    

    }

    0 讨论(0)
提交回复
热议问题