0

大家好,我有一个带有一个输入框的表单,当我提交表单时,我收到以下错误:

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

这是表格:

<?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>

谢谢

里克

4

1 回答 1

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'));
于 2012-04-17T22:56:15.580 回答