繁体   English   中英

cakephp尝试更新记录ID已存在的MySQL记录

[英]cakephp Trying to update a MySQL record where record id already exists

我正在为一个模型保存多个记录,并且如果company_id已经存在,我想更新现有记录。

我在模型中使用此代码:

function beforeSave() {

        $found = $this->find("first",array(
                "recursive" => -1,
                "conditions" => array("company_id" => $this->data['Osm']['company_id'])));
        if(isset($found))
        {
            $this->data['Osm']['id'] = $found['Osm']['id'];
        }
        return true;
    }

但出现以下错误: 错误:SQLSTATE [23000]:违反完整性约束:1062键“ PRIMARY”的条目“ 60”重复

我知道为什么会收到此错误,但似乎无法强制执行更新而不是插入。

您必须在方法而不是数据中设置$id成员变量。 $this->id = $found['Osm']['id']; 代替。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM