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 OpenGL渲染效率
  • ¥60 关于网络前后端:丢包攻击、漏洞后门、渗透这些
  • ¥30 有关于html的制作个人网页
  • ¥15 stm32 lvgl+DMA,屏幕显示不全有条纹
  • ¥15 用java,swing,mysql实现模拟ATM机系统
  • ¥15 HTML5+CSS3题
  • ¥30 使用php如何制作一个国博门票抢票系统?(相关搜索:手机号|小程序)
  • ¥15 关于#数据库#的问题:每次实验都是系统直接导出数据,但是实验过程前会修改被实验件的结构类型,软件版本(如第一次实验为结构A,A01版本软件,A01版本软件)(相关搜索:数据类型)
  • ¥15 群晖docker无法连接代理服务器
  • ¥20 VBA将EXCEL数据写入JSON,中文不显示