2011-12-06 55 views
3

我有我的UserBundle誰擴展FOSUserBundle和正常工作。但是現在,我想創建具有différents屬性的多個用戶實體。 但問題是,當我創建誰伸出我的主要用戶的實體像這樣我的用戶enttity:如何擴展FOSUserBundle的用戶實體?

class User extends BaseUser 
{ 
    protected $id; 
    // The main user class who extends FOSUser entity 
} 

class UserB extends User 
{ 
    // 
} 

當我這樣做,我得到了一個錯誤:`

Access level to MyApp\UserBundle\Entity\UserB::$id must be protected (as in class MyApp\UserBundle\Entity\User).

當我在我的用戶B實體創建一個受保護的id我有這樣的:

PHP Fatal error: Cannot redeclare MyApp\UserBundle\Entity\UserB::$id.

而結束,我不能刪除ID在我的用戶實體少返回一個錯誤學說:

[Doctrine\ORM\Mapping\MappingException]
No identifier/primary key specified for Entity 'MTS\UserBundle\Entity\User'. Every Entity must have an identifier/primary key.

有人可以幫助我嗎?

編輯:問題解決了。我的代碼:

/** 
* MTS\UserBundle\Entity\User 
* 
* @ORM\Entity 
* @ORM\InheritanceType("SINGLE_TABLE") 
* @ORM\DiscriminatorColumn(name="type", type="string") 
* @ORM\DiscriminatorMap({"userfb" = "UserFB"}) 
*/ 
abstract class User extends BaseUser 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @var string $type 
    */ 
    private $type; 
} 

/** 
* @ORM\Table() 
* @ORM\Entity() 
*/ 
class UserB extends User 
{ 
    // My variables 
} 

回答

5

您的問題似乎是缺少註釋。

我可以複製你的「每一個實體必須有一個標識符/主鍵」錯誤信息從我的工作代碼刪除此:

/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 

的作品對我來說:

/** 
* @ORM\Entity 
* @ORM\Table(name="fos_user") 
*/ 
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

感謝名單爲你的答案。最後,我的問題是由於不良繼承。 看看我的代碼的編輯。 – Naelyth