ZF2 Doctrine - Field Set Binding

I read everything I can find, but I can’t solve the problem. The problem is that data editing is attached to the form only from the basic set of fields and does not pull data from children (contact in this case). Thus, the form is populated with data from ContactAddress, but not Contact. When I upload the request to Zend \ Debug, all the information is there, but it does not make it in the form. Hopefully someone can point out that stupid mistake I'm making. Here are some pieces of code that I find relevant:

Controller:

$em = $this->getEntityManager();
$id = (int)$this->params()->fromRoute('id',0);
$form = new ContactsForm($em);

$qb = $em->createQueryBuilder();
    $qb->select('contactAddressId', 'contact')
         ->from('Application\Entity\ContactAddress', 'contactAddressId')
         ->where('contactAddressId = ' . $id)
         ->leftJoin('contactAddressId.contact', 'contact');

$query = $qb->getQuery();
$contactAddress = $query->getResult();
$form->bind($contactAddress[0]);
return array('form' => $form);

ContactsForm:

parent::__construct('orders');
$this->setAttribute('method','post')
      ->setHydrator(new ClassMethodsHydrator(false));
$this->add(array(
        'name' => 'orderId',
        'type' => 'hidden',
        'attributes' => array(
            'id' => 'orderId',
        ),
    ));

    $contactAddressFieldset = new Fieldsets\ContactAddressFieldset($objectManager);
    $contactAddressFieldset->setUseAsBaseFieldset(true);
    $this->add($contactAddressFieldset);

    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type' => 'submit',
            'value' => 'Add',
            'id' => 'submitbutton',
        ),
    ));

ContactsAddressFieldset:

parent::__construct('contactAddress');
    $hydrator = new AggregateHydrator();
    $hydrator->add(new     DoctrineHydrator($objectManager,'Application\Entity\ContactAddress'));
    $hydrator->add(new DoctrineHydrator($objectManager,'Application\Entity\ContactAddressType'));
    $hydrator->add(new DoctrineHydrator($objectManager,'Application\Entity\AddressType'));
    $this->setHydrator($hydrator);
    $this->setObject(new \Application\Entity\ContactAddress())
            ->setObject(new \Application\Entity\ContactAddressType())
            ->setObject(new \Application\Entity\AddressType());
    $this->setAttribute('method','post');

    $contactFieldSet = new ContactFieldset($objectManager);
    $this->add($contactFieldSet);


    $this->add(array(
        'name' => 'contactAddressId',
        'attributes' => array(
            'type' => 'hidden',
            'id' => 'contactAddressId',
        ),
    ));

etc.

ContactsFieldset:

parent::__construct('contact');
    $hydrator = new AggregateHydrator();
    $hydrator->add(new DoctrineHydrator($objectManager,'Application\Entity\Contact'));
    $hydrator->add(new DoctrineHydrator($objectManager,'Application\Entity\ContactType'));
    $this->setHydrator($hydrator);
    $this->setObject(new \Application\Entity\Contact())
            ->setObject(new \Application\Entity\ContactType());

    $this->setAttribute('method','post');
    $this->add(array(
        'name' => 'contactId',
        'attributes' => array(
            'type' => 'hidden',
            'id' => 'contactId',
        ),
    ));

Thanks for any help you can offer James

+4
1

, DoctrineModule , Zend Framework 2 , , composer.json

"doctrine/doctrine-orm-module": "0. *",

php composer.phar php composer.phar ORM.

: https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md

Doctrine Zend Framework 2.

, , , OneToMany /** /*.

, / :

→ (OneToMany) Fieldset → (ManyToOne) Fieldset

, , :

ContactsForm → ContactFieldset → ContactAddressFieldset

, ManyToOne , ContactAddressFieldset :

$contactFieldSet = new ContactFieldset($objectManager);
$this->add($contactFieldSet);

:

$contactFieldSet = new ContactFieldset($objectManager);
$this->add(array(
    'type' => 'Zend\Form\Element\Collection',
    'name' => 'contact',
    'options' => array(
        'count' => 1,
        'allow_add' => true,
        'allow_remove' => true,
        'target_element' => $contactFieldSet,
    )
));

Allow_add allow_remove ( true) .

, .

Yamakiroshi

+3

All Articles