Zend2 form data binding from doctrine2 ODM

Is there a better way to bind data from the doctrine2 ODM entity class to the Zend2 form, in addition, using bind() ?

If so, what would it be? Would I just get the data as an array and pass each individual field? I fight this and most likely make it harder than it should be.

When I call the bind() function, it throws a Zend error referring to the default hydrator. Do I need to do something in an entity class?

Edit: Here are the exact mistakes that 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 what I need:

  • use Zends hydrators (which I should explore how to implement) or
  • use doctrines2 hydrators (which, I would also have to find a better way to implement)
+7
source share
5 answers

In order for Zend \ Form to be able to moisten your object, you need to have something similar in your entity class:

 public function getArrayCopy() { return get_object_vars($this); } 
+14
source

in you ... / Model / XXXXTable.php
Define the function that wants to get the entry.

  $id = (int)$id; $row = $this->tableGateway->select(array('id'=>$id)); $row = $row->current(); //this line is very important 
+2
source

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

 $form = new ...; $dm = $sm->get('doctrine.documentmanager.odm_default'); $form->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($dm)); return $form; 
+2
source

I use the populate method in my object class. something like that

  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 the controller you can use the fill function this way.

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

I'm not sure if your quesiton is correctly understood.

+1
source

Fortunately, you do not need anything else in your entities if you follow this guide:

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

1.) Make sure that you check all the data that you have in your tables (or at least not null), because only verified fields are sent to the database. The author of this blog told me about this. :)

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

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

Good luck

+1
source

All Articles