2013-02-19 13 views
8

拡張プロファイルエンティティが基本UserBundleフィールドに加えて追加情報を保持できるように、FOS 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: { } 

私は大幅に異なっている具体的な情報を持つ他のいくつかのユーザータイプを持っているので、私は別々のエンティティを必要とする:私は次のように設定するエンティティを持っています私はUserBundle情報を作成しているときに、ユーザープロファイル情報を作成するために登録フォームをどのように実装すればよいですか?前もって感謝します!あなたは、エンティティの定義のためのMySQLとPHPを使用している場合

答えて

10

、あなたは次のようにあなたのエンティティを作成する必要があります。

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

最後のステップは、それがデフォルトの代わりに自分のフォームタイプを使用することFOSUserBundleを伝えるについてです
関連する問題