Zend Framework DbTable insert() 插入记录两次

发布于 2024-12-22 01:02:52 字数 1030 浏览 1 评论 0原文

我有一个如下的控制器操作,

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2024-12-29 01:02:52
The second time round the Request_Uri reports /us/account/report-comment/default.appcache?v=1 

删除主 HTML 标记(位于顶部)中的 manifest 属性以消除第二次调用。 Zend 的应用程序路由似乎正在发挥作用,而不是提供静态文件。这可能是因为该文件不存在或可能存在于不同的路径上。

The second time round the Request_Uri reports /us/account/report-comment/default.appcache?v=1 

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.

南七夏 2024-12-29 01:02:52

我有类似的问题。 ->insert 被调用两次的原因是:

public function saveAction()
{   ... 
    $result = $this->_bookModel->saveBook($data);
    if ($result) {
        $this->view->form = $form->reset();
        $this->_helper->viewRenderer('index');  
    }

我已经替换

$this->_helper->viewRenderer('index');

return $this->_helper->redirector('index'); 

并且它工作得很好。

I had a similar problem. The reason why ->insert was called twice was because of these:

public function saveAction()
{   ... 
    $result = $this->_bookModel->saveBook($data);
    if ($result) {
        $this->view->form = $form->reset();
        $this->_helper->viewRenderer('index');  
    }

I have replaced

$this->_helper->viewRenderer('index');

with

return $this->_helper->redirector('index'); 

and it worked perfect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文