2013-02-19 47 views
8

我想扩展FOS UserBundle以允许扩展配置文件实体除了基本的UserBundle字段之外还保存其他信息。由于我在网站上有多种类型的用户,我创建了独立的实体来保存配置文件信息。我已成立实体如下:将扩展配置文件实体添加到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; 

我的配置

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

我有几个其他用户类型的使用本身是截然不同的更多具体信息,所以我需要独立的实体,比如我可能有一个拥有3个地址或员工ID等的用户。鉴于此,在创建UserBundle信息时,我应该如何实现注册表来创建用户配置文件信息?提前致谢!

回答

10

如果你正在使用MySQL和PHP为您的实体定义,那么你必须创建你的实体如下:

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

第二步是创建你形成类型询问你想要的字段:

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

现在你需要让束知道你想用你的自定义窗体:

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

最后一步是如何述说FOSUserBundle,它会使用你的表单类型而不是默认的:

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

来源:

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