问题描述
我有一个数据库表 usuario (用户)和一个表 perfil ),与 perfiles_usuario (profiles_users)表连接。多对多的关系。我现在可以通过执行以下操作成功地检索用户的配置文件:$ perfiles = $ usuario-> getPerfiles();
I have a database table usuario (user) and a table perfil (profile), connected with a perfiles_usuario (profiles_users) table. A many-to-many relationship. I can now succesfully retrieve the profiles from a user by doing: $perfiles = $usuario->getPerfiles();
/**
*
* @return Perfil[]
*/
function getPerfiles()
{
$perfiles = $this->getPerfilesCollection()->toArray();
return $perfiles;
}
我想要做什么
我正在尝试制作表单,您可以在 perfil (个人资料)表中添加一个或多个现有的个人资料。我想要有一个多个选择字段,并为用户选择个人资料。
What I'm trying to do
I'm trying to make form where you can add one or more existing profiles from the perfil (profile) table. I would like to have a multiple select field and select the profiles for the user.
我的表单构建器
class UsuarioType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', 'text', array('required' => false))
->add('perfiles', 'entity', array(
'class' => 'UsuarioBundle:Perfil',
'em' => 'dynamic_em',
'query_builder' => function(EntityRepository $er){
return $er->createQueryBuilder('c')->orderBy('c.nombre', 'ASC');
},
'required' => false
))
;
}
我的观点
<div class="clearfix">
{{ form_widget(formulario.perfiles, { 'attr': { 'class': 'select-perfiles', 'multiple':'' } }) }}
</div>
我的错误:
Catchable Fatal Error: Argument 1 passed to
Centro\UsuarioBundle\Entity\Usuario::setPerfiles() must be of the type array,
object given, called in C:\....\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php
on line 345 and defined in C:\....\src\Centro\UsuarioBundle\Entity\Usuario.php line 450
Usuario .php line 450
Usuario.php line 450
public function setPerfiles(array $perfiles) // line 450
{
# Borra los perfiles que tiene el usuario
$this->getPerfilesCollection()->clear();
# Asigna los perfiles pasados por parámetro
foreach ($perfiles as $perfil) {
$this->addPerfil($perfil);
}
return $this;
}
我认为问题出现在我的用户createAction中:
I think that the problem is in my user createAction:
$usuario = new Usuario();
$formulario = $this->crearCrearForm($usuario);
$formulario->handleRequest($request); <--------- ... at this line
这是表单数据
....
centro_extranetbundle_usuario[username]:test
centro_extranetbundle_usuario[perfiles]:9
centro_extranetbundle_usuario[perfiles]:4
推荐答案
非常!这是解决方案
/**
* Devuelve una coleccion con los perfiles del usuario
*
* @return ArrayCollection
*/
function getPerfiles()
{
$perfiles = $this->getPerfilesCollection(); # ->toArray(); I delete this
return $perfiles;
}
另请更改
/**
* Establece los perfiles del usuario a partir de un ArrayCollection de
* perfiles
*
* @param ArrayColection
*
* @return self
*/
public function setPerfiles(ArrayCollection $perfiles)
{
// # Borra los perfiles que tiene el usuario
// $this->getPerfilesCollection()->clear();
// # Asigna los perfiles pasados por parámetro
// foreach ($perfiles as $perfil) {
// $this->addPerfil($perfil);
// }
// return $this;
$this->getPerfilesCollection()->clear();
foreach($collection->toArray() as $perfil){
$this->addPerfil($perfil);
}
}
在表单制作工具中,我只添加这一行: multiple'=> true,
In the form builder i only add this line: 'multiple' => true,
->add('perfiles', 'entity', array(
'class' => 'UsuarioBundle:Perfil',
'multiple' => true, <----------------------
'em' => 'dynamic_em',
'query_builder' => function(EntityRepository $er){
return $er->createQueryBuilder('c')->orderBy('c.nombre', 'ASC');
},
'required' => false
))
这篇关于多对多并形成Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!