0
votes

Here the part of the stacktrace where I have a problem:

Zend\Stdlib\Exception\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))

The action method in my Controller that call bind:

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

I checked also the BookTable class to see the object returned from the resulset and it's an istance of Book.

Than I opened the ArratSerializable.php and check the object passed and tre response is:

BookList\Model\Book Object ( [id] => 5 [author] => Gotye [title] => Making Mirrors [inputFilter:protected] => )

So it's a correct object, why doesn't it work?

1

1 Answers

1
votes

How the result is returned you generally tell that to the ResultSet object while building your model. You actually set a prototype there for returning your result set saying, hey! "Use this prototype" which is, in your case, Book model. It does have a method called getArrayCopy() which is missing. That actually rises error in this case. So please add this to the Book model thus

class Book 
{
    // other properties and methods should be here

    // add this method here
    public function getArrayCopy()
    {
        return get_object_vars($this);
    }
}