简体   繁体   English

symfony2如何用FosuserBundle覆盖User类?

[英]symfony2 how to override the User class with FosuserBundle?

My problem is quite simple but i search for an hour without succeed, i would like to add some constraint to the UserEntity. 我的问题很简单,但是我搜索了一个小时却没有成功,我想对UserEntity添加一些约束。 For example, i would like to limit the length of a username 例如,我想限制用户名的长度

I think the best way is to not touche FOS in Vendor. 我认为最好的方法是不要接触供应商中的FOS。 I have create my own userBundle with my own layout.html etc ... But i cannot override attribut who is already existing in FosuserBundle (it's working for the layout overriding btw my userBundle is a child of FOS) the funny thing is "id" has no problem for the overriding 我已经使用自己的layout.html等创建了自己的userBundle ...但是我无法覆盖FosuserBundle中已经存在的属性(它正在处理覆盖我的userBundle是FOS的子项的布局),有趣的是“ id”对于覆盖没有问题

My User entity : 我的用户实体:

<?php

namespace Diane\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**

 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Diane\UserBundle\Entity\UserRepository")
 */
class User extends BaseUser
{
    /**

     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(name="username", type="string", length=10)
     */
    protected $username;

}

Fos User model : Fos用户模型:

<?php

namespace FOS\UserBundle\Model;

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;

abstract class User implements UserInterface, GroupableInterface
{
    protected $id;

    /**
     * @var string
     */
    protected $username;


...

}

i have already try to remove : 我已经尝试删除:

/**
 * @var string
 */

Error message : 错误信息 :

Doctrine\\ORM\\Mapping\\MappingException] Duplicate definition of column 'username' on entity 'Diane\\UserBundle\\Entity\\User' in a field or discriminator column mapping. [Doctrine \\ ORM \\ Mapping \\ MappingException]在字段“ Diane \\ UserBundle \\ Entity \\ User”上的字段“用户名”或鉴别符列映射中重复定义。

Thanks for any idea you could give me 谢谢你给我的任何想法

Try AttributeOverrides Doctrine annotation: 尝试AttributeOverrides Doctrine注释:

/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Diane\UserBundle\Entity\UserRepository") / class User extends 
 *
 * @ORM\AttributeOverrides({
 *      @ORM\AttributeOverride(name="username",
 *          column=@ORM\Column(
 *              name     = "username",
 *              type     = "string",
 *              length   = 10
 *          )
 *      )
 * })
 *
 */
class User extends BaseUser
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM