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

相关推荐 更多相似问题