在FOS UserBundle中添加扩展的配置文件实体[英] Adding extended profile entity to FOS UserBundle

本文是小编为大家收集整理的关于在FOS UserBundle中添加扩展的配置文件实体的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试扩展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

来源:

overriding_forms.md

本文地址:https://www.itbaoku.cn/post/2090760.html

问题描述

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