代码之家  ›  专栏  ›  技术社区  ›  code.g

如何更新数据cakephp并获取受影响的行

  •  1
  • code.g  · 技术社区  · 8 年前

    我想这样查询sql:

    update table set `status` = 2 where id=1 and `status`=1  
    

    以及从何处获取受影响的行。我发现 doc , 但是如何更新 status 并在sql中使用where?
    我的cakephp版本是2.4.6。

    更新已解决的:
    现在我最后做的这个函数是:

    public function updateById($id,$data,$status = ''){
        if( empty($id) ) {
            return false;
        }
        $conditions = array();
        $conditions['id'] = $id;
        if($status != ''){
            $conditions['status'] = $status;
        }
        $this->updateAll($data, $conditions);
        return $this->getAffectedRows();
    

    我在控制器中调用如下函数:

    $this->MyModel->updateById(5, array('views'=>100,'status'=>2), 1);
    

    可以确保记录从 地位 1至2

    1 回复  |  直到 8 年前
        1
  •  2
  •   Alimon Karim    8 年前

    对于更新单字段,cakephp具有方法调用

    saveField()
    

    Doc

    或者可以使用方法调用

    updateAll() 
    

    Doc

    对于受影响的行,cakephp具有方法调用

    getAffectedRows()

    Doc