0
votes

Hey guys I have a form with one input box, when I submit the form I get the following error:

Catchable fatal error: Object of class Application_Model_DbTable_Comments could not be converted to string in /Users/Ric/Sites/MN/library/Zend/Db/Statement/Pdo.php on line 228 Call Stack: 0.0002 657600 1. {main}() /Users/Ric/Sites/MN/public/index.php:0 0.0323 6224344 2. Zend_Application->run() /Users/Ric/Sites/MN/public/index.php:28 0.0323 6224344 3. Zend_Application_Bootstrap_Bootstrap->run() /Users/Ric/Sites/MN/library/Zend/Application.php:366 0.0324 6224480 4. Zend_Controller_Front->dispatch() /Users/Ric/Sites/MN/library/Zend/Application/Bootstrap/Bootstrap.php:97 0.0420 7942648 5. Zend_Controller_Dispatcher_Standard->dispatch() /Users/Ric/Sites/MN/library/Zend/Controller/Front.php:954 0.0474 8519528 6. Zend_Controller_Action->dispatch() /Users/Ric/Sites/MN/library/Zend/Controller/Dispatcher/Standard.php:295 0.0475 8528200 7. ClubDescriptionController->indexAction() /Users/Ric/Sites/MN/library/Zend/Controller/Action.php:516 0.0760 12357968 8. Application_Model_DbTable_Comments->addComment() /Users/Ric/Sites/MN/application/controllers/ClubDescriptionController.php:33 0.0760 12358408 9. Zend_Db_Table_Abstract->insert() /Users/Ric/Sites/MN/application/models/DbTable/Comments.php:25 0.0790 12371776 10. Zend_Db_Adapter_Abstract->insert() /Users/Ric/Sites/MN/library/Zend/Db/Table/Abstract.php:1075 0.0791 12373808 11. Zend_Db_Adapter_Pdo_Abstract->query() /Users/Ric/Sites/MN/library/Zend/Db/Adapter/Abstract.php:575 0.0791 12373888 12. Zend_Db_Adapter_Abstract->query() /Users/Ric/Sites/MN/library/Zend/Db/Adapter/Pdo/Abstract.php:238 0.0793 12379024 13. Zend_Db_Statement->execute() /Users/Ric/Sites/MN/library/Zend/Db/Adapter/Abstract.php:479 0.0793 12379024 14. Zend_Db_Statement_Pdo->_execute() /Users/Ric/Sites/MN/library/Zend/Db/Statement.php:300 0.0793 12379104 15. PDOStatement->execute() /Users/Ric/Sites/MN/library/Zend/Db/Statement/Pdo.php:228

Here is the 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->setLabel('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));
        }
    }

Here is the controller:

public function indexAction()
    {
       //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 = $form->getValue('comment');
            $comment = new Application_Model_DbTable_Comments();
            $comment->addComment($comment);
            $this->_helper->redirector('index');
        } else {
            $form->populate($formData);
        }
    }
}

The Model:

class Application_Model_DbTable_Comments extends Zend_Db_Table_Abstract {

protected $_name = 'comments';

public function addComment($comment) {
    $data = array(
        'comment' => $comment,
        );
    $this->insert($data);
}   

}

The view:

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

Thanks

Rik

1

1 Answers

2
votes

you problem is here

$comment = $form->getValue('comment');
$comment = new Application_Model_DbTable_Comments();
$comment->addComment($comment);

change it to

$comment = new Application_Model_DbTable_Comments();
$comment->addComment($form->getValue('comment'));