2014-04-16 82 views
3

我需要重寫此Symfony \ vendor \ friendsofsymfony \ user-bundle \ FOS \ UserBundle \ Resources \ config \ doctrine \ model \ User.orm.xml文件。Symfony2覆蓋User.orm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping 
        http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> 

    <mapped-superclass name="FOS\UserBundle\Model\User"> 

     <field name="username" column="username" type="string" length="255" /> 

     <field name="usernameCanonical" column="username_canonical" type="string" length="255" unique="true" /> 

     <field name="email" column="email" type="string" length="255" /> 

     <field name="emailCanonical" column="email_canonical" type="string" length="255" unique="true" /> 

     <field name="enabled" column="enabled" type="boolean" /> 

     <field name="salt" column="salt" type="string" /> 

     <field name="password" column="password" type="string" /> 

     <field name="lastLogin" column="last_login" type="datetime" nullable="true" /> 

     <field name="locked" column="locked" type="boolean" /> 

     <field name="expired" column="expired" type="boolean" /> 

     <field name="expiresAt" column="expires_at" type="datetime" nullable="true" /> 

     <field name="confirmationToken" column="confirmation_token" type="string" nullable="true" /> 

     <field name="passwordRequestedAt" column="password_requested_at" type="datetime" nullable="true" /> 

     <field name="roles" column="roles" type="array" /> 

     <field name="credentialsExpired" column="credentials_expired" type="boolean" /> 

     <field name="credentialsExpireAt" column="credentials_expire_at" type="datetime" nullable="true" /> 

    </mapped-superclass> 

</doctrine-mapping> 

我需要改變這個字段:

<field name="email" column="email" type="string" length="255" /> 

要這樣:

<field name="email" column="email_address" type="string" length="255" /> 

我添加的getParent方法來我UserBundle。我已將Symfony \ vendor \ friendsofsymfony \ user-bundle \ FOS \ UserBundle \ Resources \ config \ doctrine \ model \ User.orm.xml 複製到我的 Bundle Symfony \ src \ Acme \ UserBundle \ Resources \ doctrine \ model \ User .orm.xml 但是當我改變它時,我沒有看到效果。我做錯了什麼?

回答

7

我已經想通了。解決方案是爲我之前創建的用戶實體添加特殊註釋,形成FOSUserBundle文檔。

* @ORM\AttributeOverrides({ 
*  @ORM\AttributeOverride(name="email", [email protected]\Column(type="string", name="email_address", length=255)), 
* }) 

全班應該是這樣的:

<?php 
namespace Acme\UserBundle\Entity; 

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

/** 
* @ORM\Entity 
* @ORM\Table(name="sf_guard_user") 
* @ORM\AttributeOverrides({ 
*  @ORM\AttributeOverride(name="email", [email protected]\Column(type="string", name="email_address", length=255)), 
* }) 
*/ 
class User extends BaseUser 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    public function __construct() 
    { 
     parent::__construct(); 
     // your own logic 
    } 
} 
+0

如何刪除從映射列?當我這樣做時,我得到錯誤「無效的映射文件」 – user3746259

+0

很抱歉遲到的迴應。這個答案有兩年了,我現在不記得了,但可能不可能用註釋來做。對不起,我沒有機會檢查它,但註釋說重寫不會刪除。如果您在商業項目中工作,我不會推薦AttributeOverride作爲使用FOSUserBundle集成的解決方案,最好自己寫,然後像我在項目中那樣使用它。 – Purzynski