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 multiple types of users on the site I have created separate entities to hold the profile information. I have the entities set up as follows:
class UserProfile
{
/**
* @var integer
*/
private $id;
/**
* @var integer
*/
private $userId;
/**
* @var string
*/
private $phone;
/**
* @var string
*/
private $language;
/**
* @var string
*/
private $company;
/**
* @var string
*/
private $salutation;
/**
* @var string
*/
private $fax;
/**
* @var string
*/
private $region;
My configuration
type: entity
table: user_profile
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
phone:
type: string
length: '15'
language:
type: string
length: '50'
company:
type: string
length: 255
salutation:
type: string
length: '5'
fax:
type: string
length: '15'
region:
type: string
length: 255
oneToOne:
userId:
targetEntity: User
joinColumn:
name: user_id
referencedColumnName: id
lifecycleCallbacks: { }
I have several other user types with more specific information that is drastically different, so I need the separate entities, for instance I may have a user with 3 addresses, or employee ids, etc. Given this, how should I implement the registration form to create user profile information when I am creating the UserBundle information? Thanks in advance!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With