0

I have an application where you view clubs, the clubs are all displayed as links and then you click a club link and get to the club description page. On this page are details of the club and a comments box for the user to add a comment about the club.

The problem I am having is when I add a comment.. I have the action to get the clubs id and display information, with the comments action both in the index action of the clubDescriptionController so they are clashing with each other.

My second problem is In my database I have a club_id(foreign key linked to primary key of clubs table "id") field in the comments table to relate to the club that the user is commenting on so when the user comments.. The comment is linked to the club of the page they are visiting, in my code I need to fix the error of the getClub action and the comments action clashing. But after that I need a point in the right direction on how to get the comments posting the clubs id into the club_id field of the comments database. That's all for the ramble, Thankyou for your time and below is the code. If you need any more information please don't hesitate to ask.

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

Comment form:

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

Comments model:

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

The view:

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

At the moment when I submit a comment it is added to the database but with the following error:

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',
)  

Thanks

Rik

Rik89
  • 157
  • 4
  • 22

2 Answers2

0

So you should give $club_id to addComment

public function addComment($comment, $club_id) {
 $data = array(
     'comment' => $comment,
     'club_id' => $club_id,
     'comment_date' => NOW(),
     );
 $this->insert($data);
 }   
}
Mateusz Rogulski
  • 7,357
  • 7
  • 44
  • 62
  • when I do that my form doesn't appear, however if I change the controller like this -> http://pastebin.com/gHaD3cRv The form displays but I get an error regarding a call to the function NOW() -> http://pastebin.com/wnvVFUGh – Rik89 Apr 18 '12 at 18:38
  • Oh, change 'NOW()' to 'new Zend_Db_Expr('NOW()')' – Mateusz Rogulski Apr 18 '12 at 18:44
  • ok great! The club_id and date are in the db, but I think there is something wrong with my foreign key configuration as I am getting this error -> http://pastebin.com/uNU7fQLj – Rik89 Apr 18 '12 at 18:53
  • http://stackoverflow.com/questions/10044062/sqlstate23000-integrity-constraint-violation-1062-duplicate-entry-1922-1-f so i think You can clean your table and then try again – Mateusz Rogulski Apr 18 '12 at 19:03
  • Its happening when I try and comment using the same club_id but I dont understand why as I haven't set this to be unique there will be comments of the same club. Also If I comment for the first time thus avoiding the mysql error I get this error -> http://pastebin.com/QMU3veJV – Rik89 Apr 18 '12 at 19:50
  • I understend you, but there http://pastebin.com/hGz5fgwS you set the club_id to unique. – Mateusz Rogulski Apr 18 '12 at 20:02
  • and the error is the same as when I use Xena's methods below -> http://pastebin.com/7uMaLrDY – Rik89 Apr 18 '12 at 20:12
  • Yes becouse this method doesn't work... look if you paste his code after 'if ($form->isValid($formData)) you lost the club_id whitch is in $id earlier – Mateusz Rogulski Apr 18 '12 at 20:23
  • In my oppinion you should 1. make other action to adding element. 2. copy all code from indexAction to addAction (without this part: $id = $form->getValue('id'); $clubs = $clubs->getClub($id); $this->view->clubs = $clubs;) 3. delete and add again table comment. If this steps don't help I don't have nothing more to say :) – Mateusz Rogulski Apr 18 '12 at 20:51
  • Ok So my controller now looks like this -> http://pastebin.com/9ybyr7fn but the comments form disappears. – Rik89 Apr 18 '12 at 21:18
  • It should looks like that http://pastebin.com/TMdHwciK (I edit only add action). And so I helped you much, I hope you appreciate it :) – Mateusz Rogulski Apr 18 '12 at 21:41
  • I do appreciate it thanks! But because the form is initialized in the addAction when I echo $this->form; The form doesn't appear.. – Rik89 Apr 18 '12 at 21:47
  • Yeah, I was tired yesterday :) now i know what you want do, and why you need only one action to do this. Use that http://pastebin.com/z15ebc6p and for sure you notice that I delete the populate because you don't need populate data there. – Mateusz Rogulski Apr 19 '12 at 08:42
  • this actually makes the application worse than when I originally posted my code, the club_id is not going in the database and the setAction part of your code is adding that action into the database instead of the comment.. – Rik89 Apr 23 '12 at 13:09
  • Ok so I have changed the code to the point where I had the club_id, date and comment in the database, as this is where the code worked the best.. The point in my code where I am at is the comment posted @ Apr 18 at 18:53 – Rik89 Apr 23 '12 at 13:36
0

The first time you get your id, use it to find the club info in your data base and set the view. When the form is submitted you now have the id stored in a form field. I think you need to query the data base again and reset the view with your new id field (this time inside the ispost condition). Either that or find a way to send id as a parameter again when you submit the form instead of sending it as a form element.

Method 1: After 'if ($form->isValid($formData)) {

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

Not the most eloquent way of doing it, but I think that would work.

Method 2: haven't tried this before, but it might work. After $form->submit->setLabel('Comment');

$form->setAction('index/club_id/'.$id);
xena
  • 163
  • 1
  • 13
  • Ok I understand what you mean, I just don't understand how to do it.. Sorry – Rik89 Apr 18 '12 at 18:24
  • OK, just added to my answer, hope it helps – xena Apr 18 '12 at 19:01
  • Xena from your first suggestion I get -> http://pastebin.com/mV3ZPhQE however the data is inserted, its still having trouble getting the club – Rik89 Apr 18 '12 at 20:00
  • You don't have id of inserted row in $form->getValue('id') – Mateusz Rogulski Apr 18 '12 at 20:34
  • I was assuming that the id from the hidden form field is the club id. Did I assume wrong? I think I may be misunderstanding what you are trying to do. I was also wondering why in your addComment function you aren't adding club_id. – xena Apr 18 '12 at 22:19
  • Yeah thats the id that is primary key for the comments table.. It still isn't working – Rik89 Apr 18 '12 at 22:41
  • well I need to echo the form in the index.phtml but the form is in the add action.. i am unsure how to do this.. – Rik89 Apr 18 '12 at 22:45
  • Ok so I have got the form to display using a view helper. -> http://pastebin.com/bSBp6aLM – Rik89 Apr 18 '12 at 23:05
  • Are you saying that you are no longer at the index view once you do the add action? If that is the case you just need to do a redirect back to index at the end of the add action. – xena Apr 18 '12 at 23:16
  • No I was saying that because the comment form wasn't initialized in the index action I could echo it from the index.phtml file, I used a helper to echo the function in the helper which initialized the form but the comments still wouldn't work. So I have decided to have the comments input form on the page add-comment.phtml created for the action addComment, the form is on the page but I am getting the following error when submitting the form -> http://pastebin.com/sWnZfyn6 – Rik89 Apr 19 '12 at 02:31