首页 文章

Zend的\ STDLIB \异常\ BadMethodCallException

提问于
浏览
0

这里是我遇到问题的堆栈跟踪的一部分:

Zend的\ STDLIB \异常\ BadMethodCallException

File: /var/www/html/zf2/vendor/zendframework/zendframework/library/Zend/Stdlib/Hydrator/ArraySerializable.php:28 Message: Zend\Stdlib\Hydrator\ArraySerializable::extract expects the provided object to implement getArrayCopy() Stack trace:

0 /var/www/html/zf2/vendor/zendframework/zendframework/library/Zend/Form/Fieldset.php(631):Zend \ Stdlib \ Hydrator \ ArraySerializable-> extract(Object(BookList \ Model \ Book))

1 /var/www/html/zf2/vendor/zendframework/zendframework/library/Zend/Form/Form.php(942):Zend \ Form \ Fieldset-> extract()

2 /var/www/html/zf2/vendor/zendframework/zendframework/library/Zend/Form/Form.php(303):Zend \ Form \ Form-> extract()

3 /var/www/html/zf2/module/BookList/src/BookList/Controller/BookController.php(59):Zend \ Form \ Form-> bind(Object(BookList \ Model \ Book))

我的Controller中调用bind的action方法:

public function editAction()
 {
   $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('book');
     }

     try {
          $book = $this->getBookTable()->getBook($id);
      }
      catch (\Exception $ex) {
          return $this->redirect()->toRoute('book', array(
              'action' => 'index'
          ));
      }

    $form  = new BookForm();
    $form->bind($book); // this is the line 59 of BookController
    $form->get('submit')->setAttribute('value', 'Edit');

    $request = $this->getRequest();
    if ($request->isPost()) {
         $form->setInputFilter($book->getInputFilter());
         $form->setData($request->getPost());

         if ($form->isValid()) {
             $this->getBookTable()->saveBook($book);

             // Redirect to list of books
             return $this->redirect()->toRoute('book');
         }
    }

    return array(
        'id' => $id,
        'form' => $form,
    );
 }

我还检查了BookTable类以查看从结果集返回的对象,它是Book的一个基准 .

比我打开ArratSerializable.php并检查传递的对象和tre响应是:

BookList \ Model \ Book Object([id] => 5 [作者] => Gotye [title] =>制作镜像[inputFilter:protected] =>)

所以它是一个正确的对象,为什么它不起作用?

1 回答

  • 1

    结果如何返回通常在构建模型时告诉 ResultSet 对象 . 你实际上在那里设置了一个原型来返回你的结果集,嘿! "Use this prototype",在您的情况下, Book 型号 . 它确实有一个名为 getArrayCopy() 的方法,它缺失了 . 在这种情况下,实际上会出现错误 . 所以请将其添加到 Book 模型中

    class Book 
    {
        // other properties and methods should be here
    
        // add this method here
        public function getArrayCopy()
        {
            return get_object_vars($this);
        }
    }
    

相关问题