2012-04-24 38 views
1

目前我有一個頁面,我通過該俱樂部的id檢索到一個俱樂部的信息。我現在有一個評論框,我想檢索關於該俱樂部的評論,在評論表中我有club_id,並且參數「club_id」被傳入此頁面。目前我正在檢索表格中的所有評論,但我只想要那個俱樂部的評論。一個正確的方向將是偉大的!Zend Framework查詢分貝和getParam

控制器:

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); 

     } 
    } 
} 

型號:

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查詢,但我越來越困惑於究竟如何?

感謝

+0

你能表現出Application_Model_DbTable_Clubs的代碼?我認爲這與你的問題有關。 – vascowhite 2012-04-24 19:22:38

回答

0

這已經有一段時間,因爲我用Db_Table但我想你想創建一個選擇對象,它允許你建立一個查詢,將選擇正確的club_id評論:

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

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

可能要按日期的意見,如果是的話,你可以做到這一點通過添加順序子句來選擇:

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

看一看的文檔進行Zend_Db_Table_Select對象,其中有相當多的例子:http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.fetch-all

+0

正確,我不知道你可以這樣做!謝謝! – Rex89 2012-04-24 22:18:53

0

在你的控制器,你在呼喚

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

應該

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

其中id變量將從url中獲取。

+0

@ user1353854讓我知道,如果這有助於你 – 2012-04-24 17:42:08

+0

我認爲應該工作,但我得到的錯誤 - > http://pastebin.com/Dj00Kq8A哦,參數是「club_id」,以避免混淆.. – Rex89 2012-04-24 17:46:33

+0

@ user1353854 can你更新你的整個模型類。 – 2012-04-24 17:50:08

0

這裏是工作負責人:

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); 
}