【问题标题】:model->save() always uses 'insert' even though id is givenmodel->save() 总是使用 'insert' 即使给出了 id
【发布时间】:2013-07-10 15:42:13
【问题描述】:

我正在使用 CakePHP v2.3.5。当我更新表格时

`users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(320) NOT NULL,
  `password` varchar(45) NOT NULL,
  `firstname` varchar(255) DEFAULT NULL,
  `lastname` varchar(255) DEFAULT NULL,
  `country` varchar(255) DEFAULT 'SG',
   PRIMARY KEY (`id`)
);

使用$this->User->save($this->request->data),它总是触发插入命令并抛出

"Integrity constraint violation: 1062 Duplicate entry '10' for key 'PRIMARY'"

request->data 对象是:

array(
    'User' => array(
        'id' => '10',
        'firstname' => 'Someone',
        'lastname' => 'Surname',
    )
)

我现在使用的完整操作是:

public function addInfo() {
    if ($this->request->is('post')) {

        $this->User->create();
        $this->User->id=(int)$this->request->data['User']['id'];
        if ($this->User->save($this->request->data)) {

        }

    }
}

而Model中的beforeSave函数是:

公共函数 beforeSave($options = array()) {

   if (isset($this->data['User']['password'])) {
       $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
   }
   return true;

}

在用户对象中,这个 id=>false 在我看来很可疑

对象(用户){ 显示字段 => '名字' 主键 => 'id' useDbConfig => '默认' useTable => '用户' id => 假 数据 => 数组( [达到最大深度] ) }

【问题讨论】:

  • 尝试在request->data 数组中使用'id'=>10(10 作为整数)
  • 感谢您编辑帖子。将 id 转换为整数,'User' => array('id' => (int) 10,'firstname' => 'Someone','lastname' => 'Surname') 但问题仍然存在。
  • 请显示您的整个控制器操作代码。仅仅引用您正在使用的保存方法并不能帮助我们帮助您。
  • 如果在保存操作之前硬编码$this->User->id = 10; 会发生什么?
  • 我硬编码了 $this->User->id,还是一样。然后我尝试硬编码用户 ID,然后是 unset($this->request->data['User']['id']),它作为新行插入。

标签: cakephp cakephp-2.3


【解决方案1】:

在保存前创建

$this->User->create();
$this->User->save($this->request->data); //with the id cast as integer

create 用于重置模型,因此即使是更新,您也应该调用该函数作为预防措施。

【讨论】:

  • 它仍然会触发“插入”查询并出现“违反完整性约束”错误。
【解决方案2】:

在保存之前尝试设置 ID:

$this->User->id = useridhere
$this->Users->save($this->request->data);

【讨论】:

  • 尝试添加 $this->User->id=(int)$this->request->data['User']['id'];保存前。并测试了 $this->User->exists($this->request->data['User']['id']) 返回 1。但仍然得到 'insert' sql 而不是 'update'
【解决方案3】:

如果您想更新一个值,而不是创建一个新值,请确保您将主键字段传递到数据数组中:

$data = array('id' => 10, 'firstname' => 'Someone', 'lastname'=> 'Surname');
// This will update User with id 10

$this->User->save($data);

【讨论】:

  • 不,我想更新行,所以我没有“创建”记录。如果我没记错的话,“创建”是指“插入”操作。
  • 它仍然触发了 'insert' sql 查询。
  • 那么我猜你在你的控制器或模型中做其他事情(beforeSave),你能否在你的初始帖子中显示你的控制器的完整操作,如果在模型中存在 beforeSave >
  • 用完整的动作编辑了我的初始帖子,在模型中保存之前和我的用户对象的一部分。
【解决方案4】:

找到原因了。我碰巧在 Model 中有一个名为 getId 的函数,而 CakePHP 也使用 getId 来检查行的存在,因此,在通过 id 检查时它总是得到否定的结果,因此它触发了插入调用而不是更新调用。感谢大家的帮助。

【讨论】:

    【解决方案5】:

    我在这里遇到了同样的问题。我尝试了不同的配置(使用 save()、saveAll()、saveMany()、saveAssociated(),使用 deep 选项),后来我发现我的表有不同的主键(主键字段名称不是“id”)。向我的模型添加主字段名称可以解决问题。

    class SomeModel extends AppModel {
    
        public $primaryKey = "different_primary_field_name_for_id";
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-11
      • 2020-02-06
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 2021-11-24
      • 2012-04-11
      • 2020-01-30
      相关资源
      最近更新 更多