Zend Framework评论框

时间:2012-04-18 17:42:57

标签: zend-framework

我有一个应用程序,您可以在其中查看俱乐部,俱乐部都显示为链接,然后您单击俱乐部链接并进入俱乐部描述页面。在此页面上有俱乐部的详细信息和用户添加评论的评论框。

我遇到的问题是当我添加评论时..我有动作获取俱乐部ID并显示信息,评论操作都在clubDescriptionController的索引操作中,因此它们彼此冲突。 / p>

我的第二个问题是在我的数据库中,我在评论表中有一个club_id(链接到俱乐部表“id”的主键的外键)字段,用于与用户评论的俱乐部相关联,以便用户评论。评论链接到他们正在访问的页面的俱乐部,在我的代码中我需要修复getClub操作的错误和评论操作冲突。但在那之后,我需要在正确的方向上找到一个关于如何将评论发布到评论数据库的club_id字段的评论。这一切都是为了漫步,谢谢你的时间和下面是代码。如果您需要更多信息,请不要犹豫。

clubDescriptionController:

<?php

class ClubDescriptionController extends Zend_Controller_Action
{

public function indexAction()
{
   $this->authoriseUser();
   //get id param from index.phtml (view)
   $id = $this->getRequest()->getParam('club_id');
   //get model and query by $id
   $clubs = new Application_Model_DbTable_Clubs();
   $clubs = $clubs->getClub($id);
   //assign data from model to view [EDIT](display.phtml)
   $this->view->clubs = $clubs;

    //action for the comments submission
   $form = new Application_Form_Comment();
    $form->submit->setLabel('Comment');
    $this->view->form = $form;
    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
        $comment = new Application_Model_DbTable_Comments();
        $comment->addComment($form->getValue('comment'));
            $this->_helper->redirector('index');
        } else {
            $form->populate($formData);
        }
    }
}

评论表:

<?php

class Application_Form_Comment extends Zend_Form
{

    public function init()
    {
        $this->setName('comment');
        $id = new Zend_Form_Element_Hidden('id');
  $id->addFilter('Int');
        $comment = new Zend_Form_Element_Text('comment');
        $comment->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');
        $this->addElements(array($id, $comment, $submit));
    }
}

评论模型:

<?php

class Application_Model_DbTable_Comments extends Zend_Db_Table_Abstract
{

protected $_name = 'comments';

public function getComment($id) {
    $id = (int) $id;
    $row = $this->fetchRow('id = ' . $id);
    if (!$row) {
        throw new Exception("Count not find row $id");
    }
    return $row->toArray();
}

public function addComment($comment) {
    $data = array(
        'comment' => $comment,
        );
    $this->insert($data);
    }   
}

观点:

<div id="comments-holder">
    <p id="comments-title">Comments</p>
    <?php
        echo $this->form;
    ?>
</div>

在我提交评论时,它会被添加到数据库中但出现以下错误:

An error occurred
Application error
Exception information:

Message: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`manchesternightlife`.`comments`, CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`club_id`) REFERENCES `clubs` (`id`))
Stack trace:

#0 /Users/R_iMac/Sites/MN/library/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#1 /Users/R_iMac/Sites/MN/library/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#2 /Users/R_iMac/Sites/MN/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `co...', Array)
#3 /Users/R_iMac/Sites/MN/library/Zend/Db/Adapter/Abstract.php(575): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `co...', Array)
#4 /Users/R_iMac/Sites/MN/library/Zend/Db/Table/Abstract.php(1075): Zend_Db_Adapter_Abstract->insert('comments', Array)
#5 /Users/R_iMac/Sites/MN/application/models/DbTable/Comments.php(25): Zend_Db_Table_Abstract->insert(Array)
#6 /Users/R_iMac/Sites/MN/application/controllers/ClubDescriptionController.php(30): Application_Model_DbTable_Comments->addComment('hey')
#7 /Users/R_iMac/Sites/MN/library/Zend/Controller/Action.php(516): ClubDescriptionController->indexAction()
#8 /Users/R_iMac/Sites/MN/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('indexAction')
#9 /Users/R_iMac/Sites/MN/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#10 /Users/R_iMac/Sites/MN/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#11 /Users/R_iMac/Sites/MN/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#12 /Users/R_iMac/Sites/MN/public/index.php(28): Zend_Application->run()
#13 {main}  

Request Parameters:

array (
  'controller' => 'club-description',
  'action' => 'index',
  'club_id' => '1',
  'module' => 'default',
  'id' => '0',
  'comment' => 'hey',
  'submit' => 'Comment',
)  

由于

里克多维

2 个答案:

答案 0 :(得分:0)

所以你应该给$ club_id添加addComment

public function addComment($comment, $club_id) {
 $data = array(
     'comment' => $comment,
     'club_id' => $club_id,
     'comment_date' => NOW(),
     );
 $this->insert($data);
 }   
}

答案 1 :(得分:0)

第一次获得您的ID时,使用它来查找数据库中的俱乐部信息并设置视图。提交表单后,您现在将id存储在表单字段中。我认为您需要再次查询数据库并使用新的id字段重置视图(这次是在ispost条件内)。您可以在提交表单时找到另一种方法将id作为参数再次发送,而不是将其作为表单元素发送。

方法1:在'if($ form-&gt; isValid($ formData))之后{/ p>

$id = $form->getValue('id'); 
$clubs = $clubs->getClub($id);
$this->view->clubs = $clubs;

这不是最有说服力的方式,但我认为这样可行。

方法2:之前没有尝试过,但可能会有效。在$ form-&gt; submit-&gt; setLabel('Comment');

之后
$form->setAction('index/club_id/'.$id);