【问题标题】:CakePHP - When editing a record, sometimes we get a duplicateCakePHP - 编辑记录时,有时我们会得到重复
【发布时间】:2012-03-19 21:39:22
【问题描述】:

现在,这种情况并不总是发生,我复制错误的努力已经失败了。

基本上,我们有一个工作系统,其中包含用户、标题、正文和截止日期。有时,从作业的编辑表单中保存会产生重复,即相同的作业(具有所有相同的内容)但具有新的 ID。

我想知道是否有人遇到过类似的错误。

这是我的 Job Controller 的编辑功能:

function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid job', true));
        }
        if (!empty($this->data)) {
            if ($this->Job->save($this->data)) {
                $this->Session->setFlash(__('The job has been saved', true));
            } else {
                $this->Session->setFlash(__('The job could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Job->read(null, $id);
        }
        $users = $this->Job->User->find('list', array('fields' => array('User.username')));
        $clients = $this->Job->Client->find('list');
        $stages = $this->Job->Stage->find('list');
        $this->set(compact('users', 'clients', 'stages'));
    }

感谢任何帮助。

【问题讨论】:

  • 当你说重复时,你的意思是右边和一个新条目都被保存了,还是右边条目没有更新,而是创建了一个副本?
  • 记录的id是否总是存在于$this->data中?
  • @G.J 是正确的。下面的答案一针见血!

标签: cakephp controller cakephp-1.3 save


【解决方案1】:

可能在您的表单中,您没有此 Job 的 id。确保你有这个在视图中:

echo $this->Form->input('id');

【讨论】:

  • 谢谢,这完全正确。我取出 ID 是为了禁止对其进行编辑。现在我将它包装在一个隐藏的 div 中,这样更容易。
  • 默认情况下,您在表单中创建的任何 ID 输入都带有 type="hidden"。没有发生?
  • 老实说,在我测试它之前,我对表单进行了更改,所以很可能,我在删除它之前错过了它。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2019-08-25
  • 2013-01-03
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多