Forms ZF2原则-字段集绑定

Forms ZF2原则-字段集绑定,forms,doctrine-orm,zend-framework2,entity,Forms,Doctrine Orm,Zend Framework2,Entity,我已经阅读了所有我能找到的东西,但似乎无法解决我的问题。问题在于编辑数据时,数据仅绑定到来自基本字段集的表单,而不从子项中提取数据(本例中为联系方式)。所以表单中填写的是ContactAddress中的数据,而不是Contact。当我将查询转储到Zend\Debug中时,所有信息都在那里,但它没有进入表单。我希望有人能指出我犯的任何愚蠢的错误。以下是我认为相关的代码部分: 控制器: $em = $this->getEntityManager(); $id = (int)$this->

我已经阅读了所有我能找到的东西,但似乎无法解决我的问题。问题在于编辑数据时,数据仅绑定到来自基本字段集的表单,而不从子项中提取数据(本例中为联系方式)。所以表单中填写的是ContactAddress中的数据,而不是Contact。当我将查询转储到Zend\Debug中时,所有信息都在那里,但它没有进入表单。我希望有人能指出我犯的任何愚蠢的错误。以下是我认为相关的代码部分:

控制器:

$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);
联系人表单:

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',
        ),
    ));
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',
        ),
    ));
等等

联系人字段集:

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',
        ),
    ));
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',
        ),
    ));
谢谢你的帮助
James

确保您使用的是doctriemodule的最新版本,如果您将Zend Framework 2与composer一起使用,请确保您的composer.json文件为

“条令/条令orm模块”:“0.*”

并运行php composer.phar自我更新 然后 php composer.phar更新 这将安装最新版本的ORM模块

同时使用以下链接:

将帮助您开始使用条令和Zend Framework 2表单和字段集

确保检查您的实体是否遵循文档中所示的类似模式,并且在添加OneToMany关系的部分时,以/**开头,而不是以/*开头

还要确保表单/字段集按以下顺序排列:

表单->(一通多用)字段集->(多通)字段集

在这里,我假设一个联系人可以有很多地址,所以您的表单顺序如下:

ContactsForm->ContactFieldset->ContactAddressFieldset

此外,在定义多通字段集时,请尝试使用表单集合,因此在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(尽管默认情况下add设置为true)将允许您从表单中干净地添加和删除项目

希望这对你的情况有所帮助


Yamakiroshi

回答得很好。我正在寻找一个解决几乎相同问题的方法。我想知道,如果让你写一个1-n和n-1关系使用联接表的例子,这是否太过分了?我的用例:Country#countryCurrency,countryCurrency#countries,countryCurrency#Currency,Currency#countryCurrency。试图找出如何在一个表单中同时使用1-n和n-1来创建一个国家和它的货币实体。对上述问题的一个小补充:CountryCurrency也是一个实体,因为它包含一个“主要”布尔值来表示该国的主要货币。