gpt4 book ai didi

具有多对多关系的 Symfony2 表单实体字段

转载 作者:行者123 更新时间:2023-12-04 02:41:45 25 4
gpt4 key购买 nike

我有一个 Doctrine 用户实体,它与角色实体具有多对多关系。我还有一个根据用户实体字段在其中显示用户详细信息的表单。我以正常方式设置表单,在我的 Controller 中使用表单生成器,并传入从数据库加载的实体实例。这一切都很好。

现在,我想要的是此表单中的选择菜单,其中包含用户分配给选定的角色,并从数据库中的可用角色中填充。我在我的 UserType 表单中创建了一个名为 roles 的字段,它具有“集合”类型并传入了一个 RoleType 表单实例。在我的 RoleType 表单中,我添加了一个类型为 entity 的字段并定义了我的角色类等。这一切都按照文档进行。这一切工作正常,它加载了填充有角色的选择菜单,但它没有选择针对用户实体保存的正确角色。

当我跟踪“角色”的表单值(或在我的角色实体字段上设置数据转换器)时,我得到的值是一个字符串,其中包含与用户关联的角色的名称。我没有得到 Role 实例或 Collection/Array。此外,如果我将角色实体字段设置为 multiple = true,我会收到来自学说数据转换器的 Expected a Doctrine\Common\Collections\Collection object. 错误。同样,这是因为它需要一个集合并获取一个字符串。

这可能与我的用户实体被水化的方式有关吗?我正在使用 $repository->findOneBy(array('id' => $id));

这是我正在做的事情的简化版本:

用户类

class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*/
public $roles;

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

public function getRoles()
{
return $this->roles->toArray();
}
}

角色类

class  Role implements RoleInterface
{
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="roles")
*/
public $users;

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

public function getUsers()
{
return $this->users;
}
}

用户表单类型

class UserType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'NameSpace\MyBundle\Entity\User',
));
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', 'hidden')
->add('roles', 'collection', array('type' => new RoleType()))
->add('save', 'submit');
}

public function getName()
{
return 'user';
}
}

角色表单类型

class RoleType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'NameSpace\MyBundle\Entity\Role',
));
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'entity', array(
'class' => 'NameSpace\MyBundle\Entity\Role',
'property' => 'name'
[multiple => true]
));
}

public function getName()
{
return 'role';
}
}

最佳答案

您没有为角色的用户实体向您的表单提供集合:

public function getRoles()
{
//this returns an array
return $this->roles->toArray();
}

表单类使用实体中的 getter 和 setter,因此您实际上返回的是一个数组,因为这是 Symfony2 安全系统所需要的。您还需要实现一个 getRolesCollection 字段并在表单中使用它:

public function getRolesCollection()
{
//this returns a collection
return $this->roles;
}

//and (updated from comment below)
->add('roles_collection', 'entity', array('type' => new RoleType()))

oro 平台做这样的事情:https://github.com/orocrm/platform/blob/master/src/Oro/Bundle/UserBundle/Form/Type/UserType.php

这篇博文也可能有帮助:http://blog.jmoz.co.uk/symfony2-fosuserbundle-role-entities/这是一个在数据库中为 FOSUserBundle 添加角色的人

关于具有多对多关系的 Symfony2 表单实体字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19774760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com