CakePHP updateAll() issues

后端 未结 2 913
傲寒
傲寒 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: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);
    

    }

提交回复
热议问题