Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Zend2 form data from doctrine2 ODM

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)
like image 212
bl4design Avatar asked Oct 23 '12 20:10

bl4design


4 Answers

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);
}
like image 132
Marcel Djaman Avatar answered Oct 31 '22 12:10

Marcel Djaman


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
like image 22
Chuxin Avatar answered Oct 31 '22 11:10

Chuxin


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;
like image 2
shukshin.ivan Avatar answered Oct 31 '22 13:10

shukshin.ivan


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.

like image 1
Developer Avatar answered Oct 31 '22 11:10

Developer