Zend framework mysql insert上的Zend重复行
出于某种原因,当我从Zend插入mysql db时,我的行会变得迟钝。我尝试过通过phpmyadmin直接插入,它工作得很好,所以这不是mysql服务器的问题 这是我使用的代码:Zend framework mysql insert上的Zend重复行,zend-framework,zend-db,Zend Framework,Zend Db,出于某种原因,当我从Zend插入mysql db时,我的行会变得迟钝。我尝试过通过phpmyadmin直接插入,它工作得很好,所以这不是mysql服务器的问题 这是我使用的代码: <?php class Model_Team extends Zend_Db_Table_Abstract { protected $_name = 'team'; public function createUser($data) { $user = $this->c
<?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(模式框)调用表单时才会发生,尽管表单post是正常的,不是AJAX请求)而不是使用createRow()您尝试过使用insert()吗
还有,我们可以看看ajax代码吗?可能是表单也被发布了?我不知道为什么您的代码在保存时会双重泵送数据库,但这不重要,因为您正在使用Row对象和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;
}
}
使用$this->insert()也会复制条目。我正在使用facebox jquery插件来处理模式框。只是为了确保:1。-你是否查看了“Firebug/Chrome开发者工具”上的网络选项卡来检查帖子是否只发送了一次?2.-您是否检查过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;
}
}