ZF2 Doctrine - 字段集绑定

4
我已经阅读了所有相关的内容,但似乎不能解决我的问题。问题在于当编辑数据时,表单只从基本fieldset绑定数据,而不是从子项(例如联系人)拉取数据。因此,表单填充了来自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',
        ),
    ));

联系人地址字段集:

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

感谢您能提供的任何帮助 詹姆斯

1个回答

3
请确保您正在使用DoctrineModule的最新版本,如果您正在使用带有composer的Zend Framework 2,请确保您的composer.json文件中包含以下内容:
"doctrine/doctrine-orm-module":"0.*"
然后运行php composer.phar self-update,紧接着运行php composer.phar update,这将安装最新版本的ORM模块。
此外,使用以下链接https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md将帮助您开始使用Doctrine和Zend Framework 2表格和字段集。
请确保检查您的实体遵循文档中显示的类似模式,并在添加OneToMany关系部分时以/**开头而不是/*。
还要确保您的表单/字段集按照以下顺序进行:
form->(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属性(尽管add默认为true)将允许您清晰地向表单添加和删除项目。

希望这能帮助您的情况。

Yamakiroshi


很好的答案。我正在寻找解决方案,几乎与您相同。不知道是否可以请您写一个示例,其中1-n和n-1关系使用联接表?我的用例:Country#countryCurrency,CountryCurrency#countries,CountryCurrency#currencies,Currency#countryCurrency。尝试弄清如何在单个表单中执行1-n和n-1以创建国家并创建其货币实体。 - rkeet
以上问题的小补充:CountryCurrency也是一个实体,因为它包含一个“primary”布尔值来表示该国家的主要货币。 - rkeet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接