我在ZF2中遇到了字段集问题,我告诉你我的问题。
这是我的表单(由AngularJS制作,而不是由ZF2制作),您可以在其中输入名称,并选择是否允许哪个页面或哪个操作(页面由操作组成)。
下面的图片部分显示了我发送给ZF2的内容:
这是我的CustomRole类的数据模型:
class CustomRole
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Users\CustomRolePagePermission", mappedBy="customRole")
*/
protected $pagePermissions;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Users\CustomRoleActionPermission", mappedBy="customRole")
*/
protected $actionPermissions;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Users\GlobalRole")
*/
protected $globalRole;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Cie", inversedBy="customRoles")
* @ORM\JoinColumn(name="cie", referencedColumnName="id_cie")
*/
protected $cie;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Users\User", mappedBy="customRole")
*/
protected $users;
...
这里是我的CustomRolePagePermission类(与CustomRoleActionPermission相近):
class CustomRolePagePermission extends PagePermission
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="App\Entity\Users\CustomRole", inversedBy="pagePermissions")
* @ORM\JoinColumn(name="custom_role_id", referencedColumnName="id", nullable=false)
*/
protected $customRole;
...
然后是抽象类PagePermission:
abstract class PagePermission
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="App\Entity\Page")
* @ORM\JoinColumn(name="page_id", referencedColumnName="id", nullable=false)
*/
protected $page;
/**
* @ORM\Column(type="boolean")
*/
protected $permission;
...
现在,对应于CustomRole类的Fieldset(我已在每个实体上创建):
class CustomRoleFieldset extends Fieldset implements InputFilterProviderInterface {
protected $serviceLocator;
public function __construct(EntityManager $entityManager) {
parent::__construct('role');
$this->setHydrator(new DoctrineHydrator($entityManager, 'App\Entity\Users\CustomRole'))
->setObject(new CustomRole());
$this->add(array('name' => 'name'));
$customRolePagePermissionFieldset = new CustomRolePagePermissionFieldset($entityManager);
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'pagePermission',
'options' => array(
'target_element' => $customRolePagePermissionFieldset
),
));
$customRoleActionPermissionFieldset = new CustomRoleActionPermissionFieldset($entityManager);
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'actionPermission',
'options' => array(
'target_element' => $customRoleActionPermissionFieldset
),
));
}
public function getInputFilterSpecification() {
return array(
'name' => array('required' => true),
'pagePermission' => array('required' => true),
'actionPermission' => array('required' => true),
);
}
}
...
这里我的字段集CustomRolePagePermissionFieldset:
class CustomRolePagePermissionFieldset extends Fieldset implements InputFilterProviderInterface {
protected $serviceManager;
public function __construct(EntityManager $entityManager) {
parent::__construct();
$this->setHydrator(new DoctrineHydrator($entityManager, 'App\Entity\Users\CustomRolePagePermission'))
->setObject(new CustomRolePagePermission());
$this->add(array('name' => 'permission'));
}
...
然后,我的控制器:
...
$customRoleForm = new CustomRoleForm($em);
$customRole = new CustomRole();
$formData = $request->getPost();
$customRoleForm->bind($customRole);
$customRoleForm->setData($formData);
if ($customRoleForm->isValid()) {
$customRole->setCie($cie);
$customRole->setGlobalRole($globalRole);
$em->persist($customRole);
$em->flush();
return $this->ok($customRole->getId());
}
...
问题
当我发送表单时,会创建CustomRole,但之前检查的页面和操作不会与创建的CustomRole链接,就像我从未勾选任何复选框一样。
我不明白它为什么没有效果,你有什么想法吗?
提前多多感谢! :)
答案 0 :(得分:1)
您的收藏品应为“pagePermissions”,以便水箱调用setPagePermissions。