2013-02-19 11 views
8

Próbuję rozszerzyć pakiet użytkowników FOS, aby umożliwić rozszerzonym jednostkom profilu przechowywanie dodatkowych informacji oprócz podstawowych pól UserBundle. Ponieważ mam wiele typów użytkowników w serwisie, utworzyłem osobne obiekty do przechowywania informacji o profilu. Mam podmioty określone w następujący sposób:Dodawanie jednostki profilu rozszerzonego do FOS UserBundle

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; 

Moja konfiguracja

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: { } 

mam kilka innych typów użytkowników z bardziej szczegółowych informacji, które są drastycznie różne, więc muszę oddzielne podmioty, na przykład ja może mieć użytkownika z 3 adresami lub identyfikatorami pracowników itp. Biorąc to pod uwagę, w jaki sposób powinienem zaimplementować formularz rejestracyjny do tworzenia informacji o profilu użytkownika podczas tworzenia informacji o UserBundle? Z góry dziękuję!

Odpowiedz

10

Jeśli używasz MySQL i PHP dla definicji podmiotów, to trzeba utworzyć podmiot, jak następuje:

<?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 
    } 
} 

Drugim krokiem jest stworzenie ty tworzą rodzaj prośbą potrzebne pola:

// 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'; 
    } 
} 

teraz trzeba dać wiązka wiedzieć, że chcesz użyć niestandardowego formularza:

# 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 } 

Ostatnim krokiem jest o mówienie FOSUserBundle że wykorzysta swój typ formularza zamiast domyślnego:

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

Źródło:

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

Powiązane problemy