2012-04-18 66 views
0

我有一個應用程序,您可以查看俱樂部,俱樂部都顯示爲鏈接,然後點擊俱樂部鏈接並進入俱樂部說明頁面。在這個頁面上是俱樂部的詳細信息和用戶添加評論的評論框。Zend框架評論框

我遇到的問題是,當我添加評論。我有行動得到俱樂部ID和顯示信息,既在,使他們相互衝突的clubDescriptionController的指標作用的意見採取行動。

我的第二個問題是在我的數據庫中,我在評論表中有一個club_id(外鍵與俱樂部表「id」的主鍵鏈接),以與用戶評論的俱樂部相關聯,因此當用戶評論評論與他們正在訪問的頁面的俱樂部相關聯,在我的代碼中,我需要修正getClub操作和評論操作衝突的錯誤。但在此之後,我需要在正確的方向上指出如何將發佈俱樂部ID的評論發佈到評論數據庫的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', 
) 

感謝

裏克

+0

您的表格「評論」的外觀如何? – 2012-04-18 17:51:34

+0

評論表 - > http://pastebin.com/hGz5fgwS – Rik89 2012-04-18 17:58:07

+0

另一方面,爲什麼要在索引操作中添加註釋?我認爲一個更好的主意是添加新的操作來添加評論。 – 2012-04-18 20:44:25

回答

0

所以,你應該給$ club_id到addComment

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

我這樣做時,我得到了下面的錯誤 - > http://pastebin.com/TQJgb1gR – Rik89 2012-04-18 18:23:10

+0

,你應該控制器更改爲:http://pastebin.com/v0AKk26P – 2012-04-18 18:30:29

+0

當我做我的窗體不出現,但是,如果我改變這樣的控制器 - > http://pastebin.com/gHaD3cRv表單顯示,但我得到一個錯誤的函數NOW() - > http://pastebin.com/wnvVFUGh – Rik89 2012-04-18 18:38:12

0

您第一次得到您的ID,用它來查找您的數據庫中的俱樂部信息並設置視圖。當提交表格時,您現在已將該ID存儲在表單字段中。我認爲你需要再次查詢數據庫並使用新的id字段重置視圖(這次是在ispost條件下)。無論是或者找到一種方式來發送id作爲參數,當您提交表單而不是將其作爲表單元素髮送。

方法1:在「如果($形式 - >的isValid($ FORMDATA)){

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

沒有這樣做的最有說服力的方式,但我認爲這是可行的。

方法2:以前沒有嘗試過,但它可能工作。 $ form-> submit-> setLabel('Comment')後;

$form->setAction('index/club_id/'.$id); 
+0

好的我明白你的意思,我只是不明白怎麼做吧..對不起 – Rik89 2012-04-18 18:24:18

+0

OK,剛添加到我的回答,希望它有助於 – xena 2012-04-18 19:01:36

+0

西娜從你的第一個建議,我得到 - > http://pastebin.com/mV3ZPhQE然而數據被插入,它仍然無法獲得俱樂部 – Rik89 2012-04-18 20:00:54