php – 将扩展配置文件实体添加到FOS UserBundle
发布时间:2020-12-13 13:26:21 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试扩展FOS UserBundle,以允许扩展的配置文件实体除了基本的UserBundle字段之外还包含其他信息.因为我在网站上有多种类型的用户,所以我创建了单独的实体来保存配置文件信息.我将实体设置如下: class UserProfile{ /** * @var integer */ private $id
|
我正在尝试扩展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 AcmeUserBundleEntity;
use FOSUserBundleEntityUser as BaseUser;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ORMTable(name="user_profile")
*/
class UserProfile extends BaseUser
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(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 AcmeUserBundleFormType;
use SymfonyComponentFormFormBuilderInterface;
use FOSUserBundleFormTypeRegistrationFormType 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: AcmeUserBundleFormTypeRegistrationFormType
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
