Zend framework Zend Framework查询数据库和getParam

Zend framework Zend Framework查询数据库和getParam,zend-framework,Zend Framework,目前,我有一个页面,在那里我通过俱乐部的id检索了俱乐部的信息。我现在有一个评论框,我想在其中检索关于该俱乐部的评论,在评论表中,我有俱乐部id,并且参数俱乐部id被传递到此页面。目前,我正在检索表中的所有评论,但我只想要该俱乐部的评论。朝着正确的方向走一点就好了 控制器: class ClubDescriptionController extends Zend_Controller_Action { public $auth = null; public function init() {

目前,我有一个页面,在那里我通过俱乐部的id检索了俱乐部的信息。我现在有一个评论框,我想在其中检索关于该俱乐部的评论,在评论表中,我有俱乐部id,并且参数俱乐部id被传递到此页面。目前,我正在检索表中的所有评论,但我只想要该俱乐部的评论。朝着正确的方向走一点就好了

控制器:

class ClubDescriptionController extends Zend_Controller_Action
{

public $auth = null;

public function init()
{
    $this->auth=Zend_Auth::getInstance();
}

http://pastebin.com/m66Sg26x
protected function authoriseUser()
{
    if (!$this->auth->hasIdentity()) {
            $route = array('controller'=>'auth', 'action'=>'index');
            $this->_helper->redirector->gotoRoute($route);

        }
    }
}
public function indexAction() {
    //authorisation
    $this->authoriseUser();
    //to get the paramter club_id to query for specific club information
    $id = (int) $this->_request->getParam('club_id', 0);

    //submit a comment
    $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($formData['comment'], $id);
        } else {
            $form->populate($formData);
        }
    }

    //initialise table
    $clubs = new Application_Model_DbTable_Clubs();
    $clubs = $clubs->getClub($id);
    $this->view->clubs = $clubs;

    //to get the comments for the club
    $comments = new Application_Model_DbTable_Comments();
    $select = $comments->select();
    $select->where('club_id = ?', $id);
    $select->order('comment_date ASC');

    $this->view->comments = $comments->fetchAll($select);
}
型号:

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, $club_id) {
    $data = array(
        'comment' => $comment,
        'club_id' => $club_id,
        'comment_date' => new Zend_Db_Expr('NOW()'),
        );
    $this->insert($data);
    }   

    public function deleteComment($id) {
    $this->delete('id =' . (int) $id);
    }
}
观点:

<div id="view-comments">
        <?php foreach($this->comments as $comments) : ?>
            <p id="individual-comment">
                <?php echo $this->escape($comments->comment);?> - 
                <i><?php echo $this->escape($comments->comment_date);?></i>
            </p>
        <?php endforeach; ?>
</div>
我意识到我将不得不使用getComment;在我的模型中使用函数,并通过id查询它,但我对如何


感谢您在控制器中呼叫

$this->view->comments = $comments->fetchAll();
应该是

$this->view->comments = $comments->getComment($this->_request->getParam('club_id'));

其中id变量将从url获取。

我已经有一段时间没有使用Db_Table了,但我认为您希望创建一个select对象,它允许您构建一个查询,以选择具有正确俱乐部id的评论:

$comments = new Application_Model_DbTable_Comments();
$select = $comments->select();
$select->where('club_id = ?', $id);

$this->view->comments = $comments->fetchAll($select);
您可能希望按日期对注释进行排序,如果是这样,您可以通过向select命令行添加order子句来完成此操作:

$select->order('comment_date ASC');

查看Zend_Db_Table_Select的文档,其中有许多示例:

这是正在工作的控制器:

class ClubDescriptionController extends Zend_Controller_Action
{

public $auth = null;

public function init()
{
    $this->auth=Zend_Auth::getInstance();
}

http://pastebin.com/m66Sg26x
protected function authoriseUser()
{
    if (!$this->auth->hasIdentity()) {
            $route = array('controller'=>'auth', 'action'=>'index');
            $this->_helper->redirector->gotoRoute($route);

        }
    }
}
public function indexAction() {
    //authorisation
    $this->authoriseUser();
    //to get the paramter club_id to query for specific club information
    $id = (int) $this->_request->getParam('club_id', 0);

    //submit a comment
    $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($formData['comment'], $id);
        } else {
            $form->populate($formData);
        }
    }

    //initialise table
    $clubs = new Application_Model_DbTable_Clubs();
    $clubs = $clubs->getClub($id);
    $this->view->clubs = $clubs;

    //to get the comments for the club
    $comments = new Application_Model_DbTable_Comments();
    $select = $comments->select();
    $select->where('club_id = ?', $id);
    $select->order('comment_date ASC');

    $this->view->comments = $comments->fetchAll($select);
}

@用户1353854让我知道这是否对你有帮助我认为这应该是有效的,但我得到了错误->哦,参数是club_id以避免混淆..更新控制器后,你会看到我可以更改->$this->view->comments=$comments->getComment$this->getParam club id->;到$this->view->comments=$comments->getComment$id;很抱歉缺少代码。请尝试此$id=int$this->\u请求->获取参数'club\u id',0;确保数据库中存在您试图访问的行。我现在发现你的逻辑有缺陷。因为club_id应该是comments表中的外键,所以应该通过外键而不是主键来获取行。如果这仍然帮不了你。发布你的db表以获得评论和俱乐部。你能在应用程序\模型\数据库表\俱乐部中显示代码吗?我想这与你的问题有关。没错,我不知道你能这样做!谢谢