有没有一种方法可以删除条令ObjectSelect的多个属性


Is there a way to remove multiple attribute to a doctrine ObjectSelect?

我有3个表,User、Roles和UserRoles。一个用户只能有一个角色,但我不想在用户表中为该角色添加一列,问题是我想在用户窗体中添加一个ObjectSelect,但它将其打印为多选,我不希望这样,我尝试添加removeAttribute('multiple'),但我收到一个错误,说:

"当元素没有将多个属性设置为布尔值true时,FormSelect不允许指定多个选定值"

添加multiple:false,两者都不起作用,所以我正在努力弄清楚,这就是代码。

用户实体:

/**
 * @ORM'ManyToMany(targetEntity="Auth'Entity'User'Roles")
 * @ORM'JoinTable(name="userRole",
 *      joinColumns={@ORM'JoinColumn(name="idUser", referencedColumnName="id", unique=true)},
 *      inverseJoinColumns={@ORM'JoinColumn(name="idRole", referencedColumnName="id")}
 *      )
 * @Annotation'Type("Zend'Form'Element'ObjectSelect")
 * @Annotation'Required({"required":"true"})     
 * @Annotation'Options({"label":"Rol","target_class":"Auth'Entity'User'Roles","property":"name"})     
 * @Annotation'Attributes({"class":"form-control"})
 **/
private $roles;
public function __construct() {
    $this->roles = new ArrayCollection();
}
public function getRoles()
{
    return $this->roles->getValues();
}

AdminController添加操作:

public function addUserAction () {
    $view = new ViewModel();
    $userEntity = new User();
    $em = $this->entityManager();
    $builder = new DoctrineAnnotationBuilder($em);
    $form = $builder->createForm($userEntity);
    $form->setHydrator(new DoctrineHydrator($em, 'Users'Entity'User'));
    $form->bind($userEntity);
    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setData($request->getPost());
        if ($form->isValid()) {
            $em->persist($userEntity);
            $em->flush();
        }
    }
    $view->form = $form;
    return $view;
}

我的观点:

$form = $this->form;
$form->setAttribute('action', $this->url('admin', array('action' => 'addUser')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('username'));
echo $this->formRow($form->get('email'));
echo $this->formRow($form->get('roles'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();

如果一个用户只能有一个角色,为什么要有多对多关系?

渲染"选择"时会出现错误。ObjectSelect值有时会通过函数validateMultiValue进行验证。

功能代码为:

protected function validateMultiValue($value, array $attributes)
{
    if (null === $value) {
        return array();
    }
    if (!is_array($value)) {
        return (array) $value;
    }
    if (!isset($attributes['multiple']) || !$attributes['multiple']) {
        throw new Exception'DomainException(sprintf(
            '%s does not allow specifying multiple selected values when the element does not have a multiple attribute set to a boolean true',
            __CLASS__
        ));
    }
    return $value;
}

按照您定义用户实体的方式,您将始终拥有一个数组:

(...)
public function __construct() {
    $this->roles = new ArrayCollection();
}
(...)

因此,它总是到达最后一个if,在那里它检查multiple属性。因为您不希望它是多个,所以您将此属性设置为false,这时就会引发错误。如果它验证了一个数组,那么选择应该是多个。

如果您更改关系,您的代码应该可以工作。你可以试试多对一。通过这种方式,从角色实体中,您可以获得具有XXX角色的所有用户。从用户端,您可以获得用户拥有的唯一角色。


编辑:

对不起,现在我明白你为什么有多对多关系了。它是与联接表的一对多关系。似乎没有一对一的联接表。。。我认为通常的方法应该是将role列放在User表中,但不能。您需要roles属性是Role实体,而不是Role