我已经阅读了我能找到的所有内容,但似乎无法解决我的问题。问题是当编辑数据仅从基本字段集绑定到表单时,并不从子节点中提取数据(在这种情况下是联系人)。因此,表单中填充了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);
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',
),
));
等等
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',
),
));
感谢您提供的任何帮助 詹姆斯
答案 0 :(得分:3)
确保使用DoctrineModule的最新版本,如果您使用Zend Framework 2和composer,请确保您的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 Forms&字段集。
确保检查您的实体是否遵循文档中显示的类似模式,并且当在OneToMany关系的部分中添加/ **时,而不是/ *。
还要确保您的表单/字段集按此顺序排列:
形式 - >(一对多)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