38

我有一个ManyToMany我闯入OneToManyManyToOne关系。我想构建一个具有复选框而不是集合的表单,并且我正在使用“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,即使它们是从表单传递的,它们也没有水合到对象中。我希望我的问题很清楚。

关于我的代码有什么问题的任何想法,或者使用复选框有更好的方法吗?

4

0 回答 0