【问题标题】:Zend duplicated rows on mysql insertZend 在 mysql 插入上重复的行
【发布时间】:2012-03-13 20:28:40
【问题描述】:

由于某种原因,当我从 Zend 执行 mysql db 插入时,我的行被重复了。我已经尝试通过 phpmyadmin 直接插入,并且效果很好,所以它不是 mysql 服务器问题。

这是我使用的代码:

<?php

class Model_Team extends Zend_Db_Table_Abstract {

    protected $_name = 'team';

    public function createUser($data) {
        $user = $this->createRow();
        $user->name = $data['name'];
        $user->title = $data['title'];
        $id = $user->save();
        return $id;
    }
}

?>

提前致谢。

编辑:

我发现这种重复只发生在我通过 AJAX(模态框)调用表单时,虽然表单发布是正常的,而不是 ajax 请求)

【问题讨论】:

  • 只是为了确定: 1.- 您是否查看了“Firebug/Chrome 开发人员工具”上的网络选项卡来检查帖子是否只发送一次? 2.- 您是否检查过 createUser 方法在您的应用中只调用了一次?

标签: zend-framework zend-db


【解决方案1】:

我不知道为什么您的代码在保存时会双重抽取数据库,但是这并不重要,因为您使用的是 Row 对象和 save()。 (save() 插入或更新)
您可能需要重新构建 createUser() 函数,以便在该行已存在时无法创建新行。

<?php

class Model_Team extends Zend_Db_Table_Abstract {

protected $_name = 'team';

public function createUser(array $data) {

    $user = $this->createRow();
    //test if user has id in the array
    if (array_key_exists('id', $data)){
        $user->id = $data['id'];
    }
    $user->name = $data['name'];
    $user->title = $data['title'];
    $user->save();
    //no need to create a new variable to return the user row
    return $user;
   }
}

此方法将创建和更新用户行。

为了进一步帮助您,我需要查看我的大多数双泵的控制器代码。

【讨论】:

  • 如果有自动增量,则不可能一直。
【解决方案2】:

您是否尝试过使用 insert() 而不是 createRow()?

/**
 * Insert array of data as new row into database
 * @param array $data associative array of column => value pairs.
 * @return int Primary Key of inserted row
 */
public function createUser($data)
{
    return $this->insert($data);
}

另外 - 我们可以看到 ajax 代码吗?可能是表单也正在发布?

【讨论】:

  • 使用 $this->insert() 也会复制条目。我正在使用 facebox jquery 插件来处理模态框。
猜你喜欢
  • 2017-01-31
  • 2012-07-14
  • 2018-09-08
  • 1970-01-01
  • 2023-03-19
  • 2018-01-04
  • 2012-11-16
  • 2019-03-30
  • 1970-01-01
相关资源
最近更新 更多