如何使用 Doctrine Hydrator 将对象绑定到具有多个字段集的 Laminas 表单
如何使用 Doctrine Hydrator 将对象绑定到层压形式?绑定函数填充基本字段集,但不会填充我的地址字段集,它是用户/成员中的映射实体。
这是我的表单代码,用于提取我的字段集。所有表单字段都是在视图中创建的,但绑定方法似乎不知道我的地址字段集。
namespace Member\Form;
use Doctrine\Laminas\Hydrator\DoctrineObject as DoctrineHydrator;
use Doctrine\Persistence\ObjectManager;
use Laminas\Form\Element\Collection;
use Laminas\Form\Form;
class MemberProfileForm extends Form
{
/**
* __construct($objectManager)
* @param \Doctrine\Persistence\ObjectManager $objectManager
*/
public function __construct(ObjectManager $objectManager)
{
parent::__construct('member-profile-form');
/**
* Set the hydrator
*/
$this->setHydrator(new DoctrineHydrator($objectManager));
/**
* Set the user base fieldset
*/
$profileFieldset = new \User\Form\ProfileFieldset($objectManager);
$profileFieldset->setUseAsBaseFieldset(true);
$this->add($profileFieldset);
/**
* Set the home address fieldset
*/
$homeAddressFieldset = new \User\Fieldset\AddressFieldset($objectManager);
$this->add($homeAddressFieldset);
/**
* Security & submit
*/
$this->add([
'name' => \Application\Fieldset\SubmitCsrfFieldset::FIELDSET_NAME,
'type' => \Application\Fieldset\SubmitCsrfFieldset::class
]);
}
}
该文档演示了如何执行一对多场景,但这是一对一的情况,我尝试的任何方法都不起作用,将表单字段留空,尽管配置文件表单字段是从基本字段集填充的。
How can I bind an object to a laminas form using Doctrine Hydrator? The bind function populates the base fieldset, but will not populate my address fieldset which is a mapped entity in the user/member.
This is my form code that pulls in my field sets. All form fields are created in the view, but the bind method doesn't seem to be aware of my address fieldset.
namespace Member\Form;
use Doctrine\Laminas\Hydrator\DoctrineObject as DoctrineHydrator;
use Doctrine\Persistence\ObjectManager;
use Laminas\Form\Element\Collection;
use Laminas\Form\Form;
class MemberProfileForm extends Form
{
/**
* __construct($objectManager)
* @param \Doctrine\Persistence\ObjectManager $objectManager
*/
public function __construct(ObjectManager $objectManager)
{
parent::__construct('member-profile-form');
/**
* Set the hydrator
*/
$this->setHydrator(new DoctrineHydrator($objectManager));
/**
* Set the user base fieldset
*/
$profileFieldset = new \User\Form\ProfileFieldset($objectManager);
$profileFieldset->setUseAsBaseFieldset(true);
$this->add($profileFieldset);
/**
* Set the home address fieldset
*/
$homeAddressFieldset = new \User\Fieldset\AddressFieldset($objectManager);
$this->add($homeAddressFieldset);
/**
* Security & submit
*/
$this->add([
'name' => \Application\Fieldset\SubmitCsrfFieldset::FIELDSET_NAME,
'type' => \Application\Fieldset\SubmitCsrfFieldset::class
]);
}
}
The documentation demonstrates how to do a one to many scenario, but this is a one to one and nothing I try works, leaving the form field blank, though the profile form fields are populated from the base fieldset.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的地址字段集应该位于您的个人资料字段集中。
您的个人资料实体中应该有一个地址属性,该属性应该映射到您的地址实体。学说对象水化器应该从你的实体中获取这种关系。
I think your address fieldset should be inside your profile fieldset.
You should have an address property inside your profile entity which should map to your address entity. Doctrine object hydrator should pick this relationship up from your entites.