Symfony2 FOSUserBundle用户实体字段覆盖

时间:2022-10-16 14:10:45

I have a problem with overriding an entity. I need the field emailCanonical to be not be unique.

我有覆盖实体的问题。我需要字段emailCanonical不是唯一的。

Here is what I've done: In my UserBundle\Resources\config\doctrine\User.orm.xml I've added the following attribute-overrides configuration, according to the Doctrine2 documentation

以下是我所做的:在我的UserBundle \ Resources \ config \ doctrine \ User.orm.xml中,我根据Doctrine2文档添加了以下属性覆盖配置

<attribute-overrides>
    <attribute-override name="emailCanonical">
        <field column="email_canonical" unique="false" name="emailCanonical" />
    </attribute-override>
</attribute-overrides>

Then I ran the following console commands

然后我运行了以下控制台命令

$ php app/console doctrine:migrations:diff
$ php app/console doctrine:migrations:migrate

Everything worked fine. emailCanonical was made non unique. But now, when I need to generate an entity in other bundles of project, I have a strange error:

一切都很好。 emailCanonical不是唯一的。但是现在,当我需要在其他项目包中生成一个实体时,我有一个奇怪的错误:

 $ php app/console doctrine:generate:entities SkyModelsBundle:Category
 Generating entity "Sky\Bundle\ModelsBundle\Entity\Category"

 [Doctrine\ORM\Mapping\MappingException]
 Invalid field override named 'emailCanonical' for class 'Sky\Bundle\UserBundle\Entity\User'.

 doctrine:generate:entities [--path="..."] [--no-backup] name

However, if I remove the override settings from xml mapping, everything works fine.

但是,如果我从xml映射中删除覆盖设置,一切正常。

7 个解决方案

#1


10  

You override using PHP annotations like so (This is supported starting from doctrine version 2.3 and above, please note that in the documentation it mentions that you cannot override types , however I was able to do so using the latest version of doctrine , at the time of writing it is 2.4.4):

你可以使用像这样的PHP注释覆盖(从doctrine 2.3及更高版本开始支持这一点,请注意在文档中它提到你不能覆盖类型,但是我能够使用最新版本的doctrine,当时写它是2.4.4):

<?php 
namespace Namespace\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use FOS\UserBundle\Model\User as BaseUser;

/**
 * User
 * @ORM\Entity
 * @ORM\AttributeOverrides({
 *     @ORM\AttributeOverride(name="id",
 *          column=@ORM\Column(
 *              name     = "guest_id",
 *              type     = "integer",
 *              length   = 140
 *          )
 *      )
 * })
 */
class myUser extends BaseUser
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

This is specified in the documentation at: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#overrides

这在以下文档中指定:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#overrides

#2


3  

Quote from official documentation, so may be its the only way.

从官方文档引用,所以可能是唯一的方法。

If you need to change the mapping (for instance to adapt the field names to a legacy database), the only solution is to write the whole mapping again without inheriting the mapping from the mapped superclass.

如果您需要更改映射(例如,使字段名称适应旧数据库),唯一的解决方案是再次编写整个映射而不继承映射超类的映射。

#3


2  

I believe the name attribute of the field tag is not required, since it is already specified for the attribute-override tag

我相信字段标记的name属性不是必需的,因为它已经为attribute-override标记指定

Try this

尝试这个

<attribute-overrides>
    <attribute-override name="emailCanonical">
        <field column="email_canonical" unique="false" />
    </attribute-override>
</attribute-overrides>

#4


1  

It seems that FOSUserBundle entities aren't imported correctly into your project. Make sure FOSUserBundle is present in "mappings" section ("doctrine" branch) of your config.yml

似乎FOSUserBundle实体未正确导入到您的项目中。确保config.yml的“mappings”部分(“doctrine”分支)中存在FOSUserBundle

doctrine:
    orm:
        entity_managers:
            default:
                connection:       default
                mappings:
                    AcmeDemoBundle: ~
                    FOSUserBundle: ~

#5


0  

Got the same bug just now, and I solved it. Doctrine throws this Mappingexception in ClassMetadataInfo when it cannot find the related property (attribute or relation) in its mapping.

刚刚得到了同样的bug,我解决了它。当Doctrine在其映射中找不到相关属性(属性或关系)时,它会在ClassMetadataInfo中抛出此Mappingexception。

So, in order to override "emailCanonical" attribute, you need to use "attribute-overrides" and "attribute-override" (as you did), and redefine php class property in your entity :

因此,为了覆盖“emailCanonical”属性,您需要使用“attribute-overrides”和“attribute-override”(就像您所做的那样),并在您的实体中重新定义php类属性:

<?php
...
class YourUser extends BaseUser
{
    ...
    /**
     * Email canonical
     *
     * @var string
     *
     * @ORM\Column(name="email_canonical", type="string", length=255, unique=false)
     */
    protected $emailCanonical;

@EDIT : using this solution causes me another bug.

@EDIT:使用此解决方案会导致另一个错误。

It solved the one about "Invalid field override named…", but I got another one with "Duplicate definition of column…" when trying to validate schema with php app/console doctrine:schema:validate command.

它解决了一个关于“无效字段覆盖命名...”的问题,但是当我尝试使用php app / console doctrine:schema:validate命令验证模式时,我得到另一个“列重复定义...”。

Any idea ?

任何想法 ?

#6


0  

Your class that extends BaseUser should be like this:

扩展BaseUser的类应该是这样的:

<?php 
namespace Namespace\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
 * User
 * @ORM\Entity
 * @ORM\Table(name="user")
 * @UniqueEntity(fields="email", message="your costum error message")
 *
 */
class myUser extends BaseUser
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

?>

#7


0  

I know this is an old post, but i found a solution, at least it worked for me, maybe it would be helpful for someone else.

我知道这是一个老帖子,但我找到了一个解决方案,至少它对我有用,也许对其他人有帮助。

Well, i had the same issue and i'm using a custom manager for the fos_user, in the declaration file config.yml under doctrine entity_manager custom manager i declared the mapping to FOS_userBundle, BUT what was missing is to tell FOS_user that we use a diffrent manager and that's by adding :

好吧,我有同样的问题,我正在使用fos_user的自定义管理器,在doctrine entity_manager自定义管理器下的声明文件config.yml中我声明了映射到FOS_userBundle,但是缺少的是告诉FOS_user我们使用了不同的经理,这是通过添加:

fos_user:
---- db_driver: orm
---- model_manager_name: MyCustom_Manager

fos_user:---- db_driver:orm ---- model_manager_name:MyCustom_Manager

#1


10  

You override using PHP annotations like so (This is supported starting from doctrine version 2.3 and above, please note that in the documentation it mentions that you cannot override types , however I was able to do so using the latest version of doctrine , at the time of writing it is 2.4.4):

你可以使用像这样的PHP注释覆盖(从doctrine 2.3及更高版本开始支持这一点,请注意在文档中它提到你不能覆盖类型,但是我能够使用最新版本的doctrine,当时写它是2.4.4):

<?php 
namespace Namespace\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use FOS\UserBundle\Model\User as BaseUser;

/**
 * User
 * @ORM\Entity
 * @ORM\AttributeOverrides({
 *     @ORM\AttributeOverride(name="id",
 *          column=@ORM\Column(
 *              name     = "guest_id",
 *              type     = "integer",
 *              length   = 140
 *          )
 *      )
 * })
 */
class myUser extends BaseUser
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

This is specified in the documentation at: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#overrides

这在以下文档中指定:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#overrides

#2


3  

Quote from official documentation, so may be its the only way.

从官方文档引用,所以可能是唯一的方法。

If you need to change the mapping (for instance to adapt the field names to a legacy database), the only solution is to write the whole mapping again without inheriting the mapping from the mapped superclass.

如果您需要更改映射(例如,使字段名称适应旧数据库),唯一的解决方案是再次编写整个映射而不继承映射超类的映射。

#3


2  

I believe the name attribute of the field tag is not required, since it is already specified for the attribute-override tag

我相信字段标记的name属性不是必需的,因为它已经为attribute-override标记指定

Try this

尝试这个

<attribute-overrides>
    <attribute-override name="emailCanonical">
        <field column="email_canonical" unique="false" />
    </attribute-override>
</attribute-overrides>

#4


1  

It seems that FOSUserBundle entities aren't imported correctly into your project. Make sure FOSUserBundle is present in "mappings" section ("doctrine" branch) of your config.yml

似乎FOSUserBundle实体未正确导入到您的项目中。确保config.yml的“mappings”部分(“doctrine”分支)中存在FOSUserBundle

doctrine:
    orm:
        entity_managers:
            default:
                connection:       default
                mappings:
                    AcmeDemoBundle: ~
                    FOSUserBundle: ~

#5


0  

Got the same bug just now, and I solved it. Doctrine throws this Mappingexception in ClassMetadataInfo when it cannot find the related property (attribute or relation) in its mapping.

刚刚得到了同样的bug,我解决了它。当Doctrine在其映射中找不到相关属性(属性或关系)时,它会在ClassMetadataInfo中抛出此Mappingexception。

So, in order to override "emailCanonical" attribute, you need to use "attribute-overrides" and "attribute-override" (as you did), and redefine php class property in your entity :

因此,为了覆盖“emailCanonical”属性,您需要使用“attribute-overrides”和“attribute-override”(就像您所做的那样),并在您的实体中重新定义php类属性:

<?php
...
class YourUser extends BaseUser
{
    ...
    /**
     * Email canonical
     *
     * @var string
     *
     * @ORM\Column(name="email_canonical", type="string", length=255, unique=false)
     */
    protected $emailCanonical;

@EDIT : using this solution causes me another bug.

@EDIT:使用此解决方案会导致另一个错误。

It solved the one about "Invalid field override named…", but I got another one with "Duplicate definition of column…" when trying to validate schema with php app/console doctrine:schema:validate command.

它解决了一个关于“无效字段覆盖命名...”的问题,但是当我尝试使用php app / console doctrine:schema:validate命令验证模式时,我得到另一个“列重复定义...”。

Any idea ?

任何想法 ?

#6


0  

Your class that extends BaseUser should be like this:

扩展BaseUser的类应该是这样的:

<?php 
namespace Namespace\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
 * User
 * @ORM\Entity
 * @ORM\Table(name="user")
 * @UniqueEntity(fields="email", message="your costum error message")
 *
 */
class myUser extends BaseUser
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

?>

#7


0  

I know this is an old post, but i found a solution, at least it worked for me, maybe it would be helpful for someone else.

我知道这是一个老帖子,但我找到了一个解决方案,至少它对我有用,也许对其他人有帮助。

Well, i had the same issue and i'm using a custom manager for the fos_user, in the declaration file config.yml under doctrine entity_manager custom manager i declared the mapping to FOS_userBundle, BUT what was missing is to tell FOS_user that we use a diffrent manager and that's by adding :

好吧,我有同样的问题,我正在使用fos_user的自定义管理器,在doctrine entity_manager自定义管理器下的声明文件config.yml中我声明了映射到FOS_userBundle,但是缺少的是告诉FOS_user我们使用了不同的经理,这是通过添加:

fos_user:
---- db_driver: orm
---- model_manager_name: MyCustom_Manager

fos_user:---- db_driver:orm ---- model_manager_name:MyCustom_Manager