2013-03-21 99 views
13

我有重寫實體的問題。 我需要字段emailCanonical不是唯一的。Symfony2 FOSUserBundle用戶實體字段覆蓋

這裏是我做了什麼: 在我UserBundle\Resources\config\doctrine\User.orm.xml我已經添加了以下attribute-overrides配置,根據Doctrine2文檔

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

然後,我跑到下面的控制檯命令

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

一切正常。 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 

但是,如果我從XML映射刪除覆蓋設置,一切工作正常。

+0

我不知道,什麼是你的問題。 – 2013-03-21 09:07:28

+0

下面是我解決它的方式..你可以試試這個: http://stackoverflow.com/a/17059918/2342137 – Tim 2013-06-12 07:35:05

+0

我有同樣的問題。我使用註釋覆蓋相同的屬性,並沒有意識到有一個問題,直到我嘗試使用教條:生成:實體。你有沒有找到解決方案? – d0001 2013-10-02 02:37:51

回答

2

我相信不需要field標籤的name屬性,因爲它已經爲attribute-override標籤中指定

試試這個

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

似乎FOSUserBundle實體無法正確導入你的項目。 確保FOSUserBundle出現在「映射」部分的config.yml官方documentation

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

引用的(「理論」分支),所以可能是其唯一的出路。

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.

+0

哦上帝o.O ..... – Kuro 2013-08-01 15:33:53

+6

它也說「另一個解決方案可以通過教條的屬性和關係覆蓋」 – httpete 2014-02-14 02:23:46

+0

自2013年6月以來並非如此.. 您可以僅覆蓋映射並仍繼承自BaseUser。正如我在這裏概述: http://stackoverflow.com/questions/22718628/fosuserbundle-override-roles-property-roles-in-acme-demobundle-entity-user/24612877#24612877 – juanmf 2014-07-07 14:38:58

0

剛剛得到同樣的錯誤,我解決了它。當它在映射中找不到相關屬性(屬性或關係)時,Doctrine在ClassMetadataInfo中拋出此Mappingexception。

因此,爲了覆蓋「emailCanonical」屬性,你需要使用「屬性覆蓋」和「屬性覆蓋」(像你一樣),並在實體重新定義的PHP類屬性:

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

@EDIT:使用這個解決方案會導致我另一個bug。

它解決了一個關於「Invalid field override named ...」的問題,但是當嘗試使用php app/console doctrine:schema:validate命令驗證架構時,我得到了另一個帶有「列的重複定義...」的問題。

有什麼想法?

+0

您是否找到解決方案? – httpete 2014-02-14 02:21:48

+0

我沒有找到比上面解釋的任何其他解決方案:/我一直在尋找這些錯誤的「乾淨」解決方案 – vince 2014-02-14 07:40:28

0

你的類,它擴展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; 
} 

?> 
10

您覆蓋使用PHP的註釋,像這樣(這是支持從學說版本2.3及以上開始,請注意,在文件中也提到了你不能重寫類型,但是我可以使用最新版本的原則來完成,在編寫它時是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", 
*   [email protected]\Column(
*    name  = "guest_id", 
*    type  = "integer", 
*    length = 140 
*   ) 
*  ) 
* }) 
*/ 
class myUser extends BaseUser 
{ 
    /** 
    * @ORM\Id() 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 
} 

這是文件中規定的:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#overrides

+1

這是對我來說正確的答案。 – Hast 2014-12-15 12:10:24

0

我知道這是舊的文章,但我找到了解決辦法,至少它爲我工作,也許這將是有益的其他人。

那麼,我有同樣的問題,我使用自定義管理器的fos_user,在聲明文件config.yml下doctrine entity_manager自定義管理器我宣佈映射到FOS_userBundle,但缺少的是告訴FOS_user我們使用不同勢經理,這是通過添加:

fos_user:
---- db_driver:ORM
---- model_manager_name:MyCustom_Manager