我有一个ManyToMany
,我与OneToMany
和ManyToOne
关系分歧。我想构建一个具有复选框而不是集合的表单,我正在使用'DoctrineObject'保护器,但它不起作用,我不知道出了什么问题。
我从我的代码中移除了所有其他不相关的字段。
角色实体:
/**
* @orm\Entity
* @orm\Table(name="roles")
*/
class RolesEntity extends HemisEntity {
/**
* @orm\Id
* @orm\Column(type="integer");
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @orm\Column(name="role_code",type="string")
*/
protected $roleCode;
/**
* @orm\OneToMany(targetEntity="RolesPermissionsEntity", mappedBy="role", cascade={"persist"})
*/
protected $rolePermissions;
public function __construct()
{
$this->rolePermissions = new ArrayCollection();
}
public function setRolePermissions($rolePermissions)
{
$this->rolePermissions = $rolePermissions;
return $this;
}
public function addRolePermissions(Collection $rolePermissions)
{
foreach ($rolePermissions as $rolePermission) {
$rolePermission->setRole($this);
$this->rolePermissions->add($rolePermission);
}
}
public function removeRolePermissions(Collection $rolePermissions)
{
foreach ($rolePermissions as $rolePermission) {
$rolePermission->setRole(null);
$this->rolePermissions->removeElement($rolePermission);
}
}
public function getRolePermissions()
{
return $this->rolePermissions;
}
}
ManyToMany表格实体(它有更多字段,所以我把它打破了):
/**
* @orm\Entity
* @orm\Table(name="roles_permissions")
*/
class RolesPermissionsEntity extends HemisEntity {
/**
* @orm\Id
* @orm\Column(type="integer");
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @orm\ManyToOne(targetEntity="RolesEntity", inversedBy="rolePermissions")
* @orm\JoinColumn(name="role_id", referencedColumnName="id")
**/
protected $role;
/**
* @orm\ManyToOne(targetEntity="PermissionsEntity", inversedBy="permissionRoles")
* @orm\JoinColumn(name="permission_id", referencedColumnName="id")
**/
protected $permission;
public function setRole($role)
{
$this->role = $role;
return $this;
}
public function getRole()
{
return $this->role;
}
public function setPermission($permission)
{
$this->permission = $permission;
return $this;
}
public function getPermission()
{
return $this->permission;
}
}
我的表单看起来像这样:
class RoleForm extends Form implements InputFilterProviderInterface
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('role');
$this->setHydrator(new DoctrineHydrator($objectManager))
->setObject(new RolesEntity());
$this->add(array(
'type' => 'Zend\Form\Element\Hidden',
'name' => 'id'
));
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'roleCode',
'options' => array(
'label' => 'Role Code'
),
));
$this->add(array(
'name' => 'rolePermissions',
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'options' => array(
'label' => 'Permissions',
'object_manager' => $objectManager,
'target_class' => 'Hemis\Fnd\PermissionsEntity',
'property' => 'permissionDesc'
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Submit',
),
));
}
public function getInputFilterSpecification()
{
return array(
'roleCode' => array(
'required' => false
),
'rolePermissions' => array(
'required' => true
)
);
}
}
问题在于,当我转储$role
时,它不包含任何rolePermissions
,即使它们从表单传递,它们也不会被水合成对象。我希望我的问题很明确。
有关我的代码有什么问题的想法,或者有更好的方法可以使用复选框吗?