Adding extended profile entity to FOS UserBundle

前端 未结 1 880
灰色年华
灰色年华 2021-02-09 02:01

I am trying to extend the FOS UserBundle to allow for extended profile entities to hold additional information in addition to the basic UserBundle fields. Because I have multip

相关标签:
1条回答
  • 2021-02-09 02:29

    If you are using MySQL and php for your entities definition, then you have to create your Entity as follow:

    <?php
    // src/Acme/UserBundle/Entity/UserProfile.php
    
    namespace Acme\UserBundle\Entity;
    
    use FOS\UserBundle\Entity\User as BaseUser;
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="user_profile")
     */
    class UserProfile extends BaseUser
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        /**
         * @var string
         */
        private $phone;
    
        /**
         * @var string
         */
        private $language;
    
        //....................
        //Add all your properties here
    
        public function __construct()
        {
            parent::__construct();
            // your own logic
        }
    }
    

    The second step is to create you form type asking the fields you want:

    // src/Acme/UserBundle/Form/Type/RegistrationFormType.php
    <?php
    
    namespace Acme\UserBundle\Form\Type;
    
    use Symfony\Component\Form\FormBuilderInterface;
    use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
    
    class RegistrationFormType extends BaseType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            parent::buildForm($builder, $options);
    
            // add your custom field
            $builder->add('phone');
            $builder->add('language');
    
            //...............
            //Add all your properties here with $builder->add('property name')
        }
    
        public function getName()
        {
            return 'acme_user_registration';
        }
    }
    

    Now you need to let the bundle know that you want to use your custom form:

    # src/Acme/UserBundle/Resources/config/services.yml
    services:
        acme_user.registration.form.type:
            class: Acme\UserBundle\Form\Type\RegistrationFormType
            arguments: [%fos_user.model.user.class%]
            tags:
                - { name: form.type, alias: acme_user_registration }
    

    The last step is about telling FOSUserBundle that it will use your form type instead of the default one:

    # app/config/config.yml
    fos_user:
        # ...
        registration:
            form:
                type: acme_user_registration
    

    Source:

    https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#step-3-create-your-user-class
    https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md

    0 讨论(0)
提交回复
热议问题