Zend Framework DbTable insert() 插入记录两次
我有一个如下的控制器操作,
public function reportcommentAction() {
$comment_id = $this->getRequest()->comment_id;
$blockedCommentTable = new Application_Model_DbTable_BlockedComments();
$blockedCommentTable->blockComment($comment_id, $this->user_id);
}
它调用 blockComment() dbTable 模型,如下所示
class Application_Model_DbTable_BlockedComments extends Zend_Db_Table_Abstract {
protected $_name = 'blocked_comments';
public function blockComment($comment_id, $blocked_by) {
if (!empty($comment_id) && !empty($blocked_by)) {
$data = array(
'comment_id' => $comment_id,
'blocked_by' => $blocked_by
);
$this->insert($data);
exit;
}
}
出于某种原因,我需要该退出;在最后。如果没有它,我会插入 2 条记录,而不是按预期插入一条记录。
我在blocked_comments 表中有3 个字段,即id、comment_id 和blocked by。使用 exit 语句后,我会按预期获得值为 1, 21, 1 的记录。由于某种原因,如果没有 exit 语句,我会获得值为 2、0、1 的额外记录。
我在代码的其他部分有相同的代码(没有多余的退出),我不知道这里发生了什么。
I have a controller action as follows
public function reportcommentAction() {
$comment_id = $this->getRequest()->comment_id;
$blockedCommentTable = new Application_Model_DbTable_BlockedComments();
$blockedCommentTable->blockComment($comment_id, $this->user_id);
}
which makes a call to the blockComment() dbTable model which looks like this
class Application_Model_DbTable_BlockedComments extends Zend_Db_Table_Abstract {
protected $_name = 'blocked_comments';
public function blockComment($comment_id, $blocked_by) {
if (!empty($comment_id) && !empty($blocked_by)) {
$data = array(
'comment_id' => $comment_id,
'blocked_by' => $blocked_by
);
$this->insert($data);
exit;
}
}
For some reason, I need that exit; at the end. Without it I get 2 records inserted instead of just the one as expected.
I have 3 fields in the blocked_comments table, i.e. id, comment_id and blocked by. With the exit statement in place I get a record with values 1, 21, 1 as expected. Without the exit statement I get an extra record with values 2, 0, 1 for some reason.
I have the same code ( without the superfluous exit) working in other parts of my code and I have no idea what is going on here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除主 HTML 标记(位于顶部)中的
manifest
属性以消除第二次调用。 Zend 的应用程序路由似乎正在发挥作用,而不是提供静态文件。这可能是因为该文件不存在或可能存在于不同的路径上。Remove the
manifest
attribute in the main HTML tag (right at the top) to get rid of the second call. Zend's application routing seems to be kicking in instead of serving the static file. This could be because the file does not exist or might exist on a different path.我有类似的问题。
->insert
被调用两次的原因是:我已经替换
为
并且它工作得很好。
I had a similar problem. The reason why
->insert
was called twice was because of these:I have replaced
with
and it worked perfect.