ZendFramework:类的对象无法转换为字符串

时间:2012-04-17 22:53:10

标签: zend-framework zend-form

嘿伙计我有一个带有一个输入框的表单,当我提交表单时出现以下错误:

捕获致命错误:无法将类Application_Model_DbTable_Comments的对象转换为第228行的/Users/Ric/Sites/MN/library/Zend/Db/Statement/Pdo.php中的字符串。调用堆栈: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_D bTable_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

以下是表格:

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

这是控制器:

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

模特:

类Application_Model_DbTable_Comments扩展Zend_Db_Table_Abstract {

protected $_name = 'comments';

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

}

观点:

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

由于

里克多维

1 个答案:

答案 0 :(得分:2)

你的问题在这里

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

将其更改为

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