7

Is there a better way to bind data from a doctrine2 ODM entity class to a Zend2 form besides using bind()?

If so, what would it be? Would I just retrieve the data as an array and pass each individual field? I am struggling with this and most likely making it harder than it needs to be.

When I call the bind() function it outputs a Zend error referencing the default hydrator. Do I need to do something special in my entity class?

Edit: Here are the exact errors Zend is throwing

~\vendor\zendframework\zendframework\library\Zend\Stdlib\Hydrator\ArraySerializable.php:35

Zend\Stdlib\Hydrator\ArraySerializable::extract expects the provided object to implement getArrayCopy()

They make me think I need to either:

  1. use Zends hydrators (which I'd have to research how to implement) or
  2. use doctrine2's hydrators (which, I'd also have to figure out the best way to implement)
user987339
  • 10,519
  • 8
  • 40
  • 45
bl4design
  • 117
  • 1
  • 8
  • Please modify your question to contain the actual error message. The way ZF2 works you bind an object/entity to your form and then the assigned Hydrator makes sure the referenced Form-Fields are mapped appropriately. This actually is a very simple use-case and a good separation of concerns, as given by error messages you'll always know WHERE to look. – Sam Oct 24 '12 at 06:24

5 Answers5

14

For Zend\Form being able to hydrating your entity you need to have something like that in your entity class:

public function getArrayCopy()
{
    return get_object_vars($this);
}
Marcel Djaman
  • 1,276
  • 1
  • 17
  • 34
  • 1
    Was intrigued by the get_object_vars function. Reference: [get_object_vars](http://php.net/manual/en/function.get-object-vars.php) - "Gets the accessible non-static properties of the given object according to scope." – Dmytro Dzyubak Dec 18 '14 at 01:16
  • 1
    For more clearly, We add above method to this file: `\zf2-tutorial\module\Album\src\Album\Model\Album.php`. – Vy Do Mar 01 '15 at 10:14
2

in you .../Model/XXXXTable.php
define a function want to get a record.

    $id = (int)$id;
    $row = $this->tableGateway->select(array('id'=>$id));
    $row = $row->current();    //this line is very important
Chuxin
  • 89
  • 1
  • 6
2

I use the following code at module.config.php to use doctrine hydrator

$form = new ...;
$dm = $sm->get('doctrine.documentmanager.odm_default');
$form->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($dm));
return $form;
shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
1

I use the populate method in my entity class. something like this

  public function populate($data = array())
    {
        $this->id = ( isset($data['id'])) ? $data['id'] : null;
        $this->username = (isset($data['username'])) ? $data['username'] : null;
        $this->pass = (isset($data['pass'])) ? $data['pass'] : null;               

    }

and then in controller you can use populate function something like this.

$user = new User();
$request = $this->getRequest();
$user->populate($request->getPost());

I am not sure if I understand your quesiton correctly.

Developer
  • 25,073
  • 20
  • 81
  • 128
  • Basically, my questions pertains to an editAction(). I want users to be able to edit information they've already posted, obviously. I've done some research and found Zend uses a bind() method that will pull data from my entity and populate the form to edit. Currently I'm doing something like $form = new Form(); $form->bind($entity). My $entity is using doctrne2's getServiceLocator() and I build a query using getQueryBuilder(). When I navigate to my editAction I get the following error Zend\Stdlib\Hydrator\ArraySerializable::extract expects the provided object to implement getArrayCopy() – bl4design Oct 24 '12 at 17:52
1

Fortunately, you do not need anything more in your entities if you follow this tutorial:

http://samminds.com/2012/07/a-blog-application-part-1-working-with-doctrine-2-in-zend-framework-2/ I use nearly the same approach and it is (almost) working well. Two things you should care:

1.) Be sure you validate all data you have in your tables (or at least the not null ones), because only validated fields are sent to the DB. It was said to me by the author of this blog. :)

2.) When you create the view for the edit form, add the id to the route:

$form->setAttribute('action', $this->url('post',
                    array('action'=>'add', 'id'=>"$this->id")))->prepare();

Good luck!

Mate Kocsis
  • 126
  • 1
  • 8