dthtvk3666 2014-01-16 21:22
浏览 25
已采纳

多对多并形成Symfony2

What i have

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;
}

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.

What I tried to do

My form builder

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
            ))

        ;
    }

My view

<div class="clearfix">
    {{ form_widget(formulario.perfiles, { 'attr': { 'class': 'select-perfiles', 'multiple':'' } }) }} 
</div>

My error:

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

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;
}

I think that the problem is in my user createAction:

$usuario = new Usuario();
$formulario = $this->crearCrearForm($usuario);
$formulario->handleRequest($request); <--------- ... at this line

And this is the Form Data

....
centro_extranetbundle_usuario[username]:test
centro_extranetbundle_usuario[perfiles]:9
centro_extranetbundle_usuario[perfiles]:4
  • 写回答

2条回答 默认 最新

  • dongmaopan5738 2014-01-17 15:55
    关注

    Thanks so much! here is the solution

    /**
     * Devuelve una coleccion con los perfiles del usuario
     *
     * @return ArrayCollection
     */
    function getPerfiles()
    {
    
        $perfiles = $this->getPerfilesCollection(); # ->toArray(); I delete this
        return $perfiles;
    }
    

    Also i change

    /**
     * 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);
        }
    }
    

    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
            ))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器