Zend Framework DbTable insert() inserting records

2019-08-02 10:11发布

问题:

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.

回答1:

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.



回答2:

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.