doujiang1913 2014-07-08 00:14
浏览 13
已采纳

在Symfony2中处理用户和配置文件的方法

I'm working on a Symfony2.5 project with FOSUserBundle and I need to add the ability to create users profile from the same register form. Right now I have two entities SysUsuario and SysPerfil. This is the code for SysUsuario:

class SysUsuario extends BaseUser
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="SysGrupos")
     * @ORM\JoinTable(name="sys_usuarios_grupos",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     */
    protected $groups;

}

And this is the code (relevant part just the attributes) for SysPerfil:

class SysPerfil
{

    /**
     * @var integer
     *
     * @ORM\Column(name="idperfil", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $idperfil;

    /**
     * @var string
     *
     * @ORM\Column(name="pers_juridica", type="string", length=1, nullable=false)
     */
    protected $persJuridica = 'V';

    /**
     * @var boolean
     *
     * @ORM\Column(name="rif", type="boolean", nullable=false)
     */
    protected $rif;

    /**
     * @var boolean
     *
     * @ORM\Column(name="ci", type="boolean", nullable=false)
     */
    protected $ci;

    /**
     * @var string
     *
     * @ORM\Column(name="nombre", type="string", length=50, nullable=true)
     */
    protected $nombre;

    /**
     * @var string
     *
     * @ORM\Column(name="apellido", type="string", length=50, nullable=true)
     */
    protected $apellido;

    /**
     * @ORM\ManyToOne(targetEntity="SysUsuario")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="user", referencedColumnName="id")
     * })
     */
    protected $user;

}

I have created also two Form: UsuarioType and PerfilType:

UsuarioType

class UsuarioType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
                ->add('perfil', new PerfilType())
                ->add('email', 'email', array(
                    'required' => true,
                    'label' => 'Email',
                    'trim' => true
                ))
                ->add('password', 'password', array(
                    'required' => true,
                    'label' => 'Contraseña',
                    'always_empty' => true
                ))
                ->add('confirm', 'password', array(
                    'required' => true,
                    'mapped' => false,
                    'label' => 'Verificar contraseña',
                    'always_empty' => true
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
                array(
                    'data_class' => 'Sunahip\UserBundle\Entity\SysUsuario',
                    'intention' => 'new_user'
                )
        );
    }

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

}

PerfilType

class PerfilType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
                ->add('persJuridica', 'choice', array(
                    'choices' => array('0' => 'V', '1' => 'J'),
                    'required' => true,
                    'label' => 'RIF',
                    'trim' => true
                ))
                ->add('rif', 'text', array(
                    'required' => true,
                    'label' => false,
                    'trim' => true
                ))
                ->add('ci', 'text', array(
                    'required' => true,
                    'label' => 'CI',
                    'trim' => true
                ))
                ->add('nombre', 'text', array(
                    'required' => true,
                    'label' => 'Nombre',
                    'trim' => true
                ))
                ->add('apellido', 'text', array(
                    'required' => true,
                    'label' => 'Apellidos',
                    'trim' => true
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
                array(
                    'data_class' => 'Sunahip\UserBundle\Entity\SysPerfil',
                    'intention' => 'new_user_profile'
                )
        );
    }

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

}

But I'm getting this error:

Neither the property "perfil" nor one of the methods "getPerfil()", "perfil()", "isPerfil()", "hasPerfil()", "__get()" exist and have public access in class "Sunahip\UserBundle\Entity\SysUsuario".

What's wrong on my approach? How did yours do this?

  • 写回答

2条回答 默认 最新

  • doujingjiao0015 2014-07-08 06:20
    关注

    read the exception message.

    Well ... there's obviously no property $perfil that's mapped to SysPerfil in your SysUsuario class.

    That's exactly what the exception message hints you at.

    If a user doesn't have a variable/method to store his profile ... you can't add one with a form, right?

    solution:

    Add this to your SysUsuario class to solve this issue.

    /**
      * @ORM\OneToMany(targetEntity="SysPerfil")
      */
    protected $perfil;
    
    public function setPerfil($perfil)
    {
       $this->perfil = $perfil;
    
       return $this;
    }
    

    Don't forget to clear your cache afterwards.

    Another advice:

    You should definitely be using english class-, method- and property-names ... even if you don't plan to collaborate with someone who doesn't speak your language ... it will make your code easier to understand for anyone... even if it's just in stackoverflow questions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上