Zend Framework 2(ZF2)&使用ManyToMany关联的动态形式的Doctrine 2

时间:2015-03-27 10:42:33

标签: forms collections doctrine-orm zend-framework2

使用表单更新具有多个权限的角色的最佳做法是什么?

我的角色表:

+----+-------+
| id | name  |
+----+-------+
|  1 | admin |
|  2 | user  |
+----+-------+

我的权限表:

+----+------------+
| id | name       |
+----+------------+
|  1 | createUser |
|  2 | updateUser |
|  3 | createRole |
|  4 | updateRole |
+----+------------+

我的ManyToMany关联表, role_permission

+---------+---------------+
| role_id | permission_id |
+---------+---------------+
|       1 |             1 |
|       1 |             2 |
|       1 |             3 |
|       1 |             4 |
|       2 |             2 |
+---------+---------------+

我的目标是使用单选按钮显示所有角色,并使用复选框动态显示相关权限。

我的问题图片: image

单击admin角色时,不会使用所选角色更新权限(当然)。这让我回到我的问题:使用ZF2 + Doctrine 2动态显示所选角色的正确权限的最佳做法是什么?

这是我生成表单的方式:

use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Doctrine\ORM\EntityManager;

class EditRoleForm extends Form implements InputFilterAwareInterface
{

    private $editRoleInputFilter;
    private $entityManager;
    private $roleRepository;
    private $permissionRepository;

    public function __construct(EntityManager $entityManager) {
        parent::__construct('editRole');
        $this->editRoleInputFilter = new InputFilter();
        $this->entityManager = $entityManager;
        $this->roleRepository = $entityManager->getRepository('Authorization\Entity\Role');
        $this->permissionRepository = $entityManager->getRepository('Authorization\Entity\Permission');

        $this->add(array(
            'name' => 'roles',
            'type' => 'DoctrineModule\Form\Element\ObjectRadio',
            'options' => array(
                'label' => 'Roles',
                'label_attributes' => array(
                    'class' => 'col-sm-12'
                ),
                'object_manager' => $this->entityManager,
                'target_class' => 'Authorization\Entity\Role',
                'property' => 'name',
                'is_method' => true,
                'find_method' => array(
                    'name' => 'findAll',
                    'params' => array(
                        'orderBy' => array('id' => 'ASC'),
                    ),
                ),
            ),
        ));

        $this->add(array(
            'name' => 'permissions',
            'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
            'options' => array(
                'label' => 'Permissions',
                'label_attributes' => array(
                    'class' => 'col-sm-12'
                ),
                'object_manager' => $this->entityManager,
                'target_class' => 'Authorization\Entity\Permission',
                'property' => 'name',
                'is_method' => true,
                'find_method' => array(
                    'name' => 'findAll',
                    'params' => array(
                        'orderBy' => array('id' => 'ASC'), // Sorting won't work
                    ),
                ),
            ),
        ));

        $this->add(array(
            'name' => 'submit',
            'type' => 'Submit',
            'attributes' => array(
                'value' => 'Update role',
                'id' => 'submitEditRoleButton',
                'class' => 'btn btn-white'
            ),
        ));

        // assign input filters and validators
        $this->editRoleInputFilter->add(array(
            'name' => 'permissions',
            'required' => true,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
        ));


        $this->setInputFilter($this->editRoleInputFilter);
    }

}

如您所见,我不知道如何将复选框链接到所选的单选按钮。

0 个答案:

没有答案