问题描述
我一直跟随 Doctrine Hydrator教程,但是当我的字段集包含一个ObjectSelect时,我在保存时遇到了问题.我在实体上使用ORM映射.基本上我有一个带有id
和name
的Role
实体.我还有一个User
实体,其中包含id
,name
和role
(ManyToOne).我也有我的吸气剂和二传手.我的setRole()
方法将Role
实体作为参数传递.
I was following along with the Doctrine Hydrator tutorial, but I am having issues saving when my fieldset contains an ObjectSelect. I'm using ORM mapping on my entities. Basically I have a Role
entity with id
and name
. I also have a User
entity with id
, name
and role
(ManyToOne). I also have my getters and setters. My setRole()
method passes the Role
entity as a parameter.
/** @param Role $role */
public function setRole(\Application\Entity\Role $role) {
$this->role = $role;
}
我设置了带有教义水合器的UserFieldset.
I setup a UserFieldset with a Doctrine Hydrator.
$this->setHydrator(new DoctrineHydrator($objectManager, 'Application\Entity\User'))
->setObject(new User());
为角色选择的对象
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'role',
'options' => array(
'label' => 'Role',
'object_manager' => $objectManager,
'target_class' => 'Application\Entity\Role',
'property' => 'name'
)
));
然后我设置一个UserForm,该UserForm设置DoctrineHydrator并添加UserFieldset.
I then setup a UserForm that sets the DoctrineHydrator and adds the UserFieldset.
我的控制器动作
public function addUserAction() {
$objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$form = new UserForm($objectManager);
$user = new User();
$form->bind($user);
if ($this->request->isPost()) {
$form->setData($this->request->getPost());
if ($form->isValid()) {
$objectManager->persist($user);
$objectManager->flush();
}
}
return array('form' => $form);
}
似乎正在发生的是,角色的ID传递给setRole而不是对象.作为解决方法,我已将操作修改为:
What seems to be happening is that the ID of the role is passed to setRole rather than an object. As a workaround I've modified my action to:
if ($form->isValid()) {
$objectManager->persist($user);
$data = $this->request->getPost();
$role = $objectManager->find('Application\Entity\Role', $data->user['role']);
$user->setRole($role);
$objectManager->flush();
}
似乎不需要执行此附加步骤,但是我不确定是否需要修改setRole或是否还需要将Role实体绑定到表单.这显然是一个简化的示例,但是我的实际形式具有许多关联,因此必须像这样在控制器中进行编码很繁琐.
It seems as if this additional step should not be required, but I am not sure if I need to modify my setRole or if I also need to bind a Role entity to the form. This is obviously a simplified example, but my actual forms have many associations that will be tedious to have to code in the controller like this.
更新:调试有关帖子和表单的信息.
UPDATE:Debug information about post and form.
var_dump($form->getData());
var_dump($this->request->getPost());
输出
object(Application\Entity\User)[395]
protected 'id' => int 6
protected 'name' => string 'Jane Doe' (length=8)
protected 'role' => null
object(Zend\Stdlib\Parameters)[146]
public 'user' =>
array (size=3)
'id' => string '' (length=0)
'name' => string 'Jane Doe' (length=8)
'role' => string '3' (length=1)
public 'submit' => string 'Add User' (length=8)
推荐答案
终于,我开始使用它了.问题是我需要将角色添加到字段集
At long last I got it working. The issue was that I needed to add the role to my input filter on the fieldset
public function getInputFilterSpecification() {
return array(
'name' => array('required' => true),
'role' => array('required' => true)
)
}
...以及表单上的验证组.
... and my validation group on my form.
$this->setValidationGroup(array(
'User' => array(
'name',
'role'
)
));
现在要将用户保存在我的操作中,就是这样
Now to save the user in my action it is simply
if ($form->isValid()) {
$objectManager->persist($user);
$objectManager->flush();
}
这篇关于使用Zend Form保存包含ObjectSelect元素的Doctrine 2实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!