CakePHP saveAll- 在 mysql 中更新一行而不是另存为新行 [英] CakePHP saveAll- Updating a row in mysql instead of saving as a new row

查看:80
本文介绍了CakePHP saveAll- 在 mysql 中更新一行而不是另存为新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试复制表单中的特定条目.我通过表单 ID 获取所有列值,并尝试使用 saveAll 将其另存为新行.但它不是创建新行,而是更新现有条目.

I'm trying to duplicate a particular entry in the form. I get all the column values by their form id, and trying to save it as a new row using saveAll. But instead of creating a new row, it is updating the existing entry.

这是我的代码:

function duplicateForm($data)
   {
         $this->data['Form']['id']=$data['Form']['id'];
         $existingForm=$this->find('all',array('conditions'=>array('Form.id'=>$this->data['Form']['id'])));

         foreach($existingForm as $ex):
             $this->data['Form']['name']=$ex['Form']['name']." (copy)";
              $this->data['Form']['created_by']=$ex['Form']['created_by'];
               $this->data['Form']['access']=$ex['Form']['access'];
                 $this->data['Form']['status']='Incompleted';
                if($this->saveAll($this->data)) 
                {echo "Success";}
         endforeach;
   } 

我想也许因为表单名称相同,它正在更新.所以我在表单名称中添加了copy"字符串.然而,旧条目只是得到更新.

I thought maybe since the Form name is same, it is getting updated. So I added the 'copy' string to the name of the form. Yet the old entry is only getting updated.

这些是我的表格表单"的列及其类型:

These are the columns of my table 'Form' and their type:

    Field            Type   

  id                int(11)             
  name              varchar(25)     
  created_by        int(11)             
  access            varchar(10)     
  created           timestamp           
  modified          timestamp               
  status            varchar(15) 

id 字段是自动递增的,所以我想如果我给一个 save 方法,id 会自己生成.

The id field is auto-increment,so I thought that If I give a save method, the id will be generated of its own.

推荐答案

Cake 根据 id 字段的存在和这个 id 在数据库中.由于您包含 id,它会始终更新现有记录.

Cake decides whether to update an existing record or save data as a new one by the presence of the id field and the existence of this id in the database. Since you include the id, it'll always update the existing record.

你可以像这样大大简化你的代码:

You could greatly simplify your code like this:

$form = $this->read(null, $data['Form']['id']);

unset($form['Form']['id']);  // no id == new record
$form['Form']['status'] = 'Incompleted';

$this->create();
$this->save($form);

作为一般建议:不要将数组的内容一个接一个地复制到另一个数组中,特别是如果它是您的模型数据.首先,这很乏味,但更重要的是,如果您在 Form 表中添加了新列,您要么必须寻找所有应复制该新字段的位置,或者,更有可能的是,您会引入有趣的错误,因为每当您复制 Form 时,新字段都丢失了.

As general advice: Don't copy contents of arrays one by one into another array, especially if it's your model data. First of all it's tedious, but more importantly, if you ever add a new column to your Form table, you'll either have to hunt down all the places where that new field should be copied as well, or, more likely, you'll introduce funny bugs because that new field is missing whenever you duplicate a Form.

这篇关于CakePHP saveAll- 在 mysql 中更新一行而不是另存为新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆