Monday, April 23, 2012

ZendFramework, using same variable twice causing null value

Hi I have a application where I have a list of clubs once the club is clicked it goes to a description page of this club by passing the variable "club_id" and then getting the information for that specific club. On this page is a comments box, at the moment If I click the comment button I get the data I need in the comments box but then when trying to redirect to the index page I get the error below:



Error -> http://pastebin.com/VscHSGXF



clubDescriptionController:



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;

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


ClubDescription Index.phtml:



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


Thanks



Rik





No comments:

Post a Comment