簡體   English   中英

將擴展配置文件實體添加到FOS UserBundle

[英]Adding extended profile entity to FOS UserBundle

我正在嘗試擴展FOS UserBundle,以允許擴展的配置文件實體除了基本的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信息時創建用戶配置文件信息? 提前致謝!

如果您使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM