2013-01-31 77 views
1

SF2和Doctrine有一個奇怪的問題。當試圖從app.php運行我的應用程序我得到這個:Symfony2 Docrtine - 每個實體都必須有一個標識符/主鍵 - 它有一個主鍵?

[2013-01-31 15:40:05] request.CRITICAL: Doctrine\ORM\Mapping\MappingException: No identifier/primary key specified for Entity 'ACME\MyBundle\Entity\MyEntity'. Every Entity must have an identifier/primary key. (uncaught exception) at /lib/Doctrine/ORM/Mapping/MappingException.php line 37 [] [] 

這似乎相當自我解釋。但據我所看到的,我有我的實體主鍵:

我的實體:

/** 
* @ORM\Entity 
* @ORM\Table(name="milestone") 
*/ 
class MyEntity 
{ 

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

    /** 
    * @ORM\Column(type="string") 
    * @Assert\NotBlank() 
    * @Assert\MinLength(2) 
    * @Assert\MaxLength(100) 
    */ 
    protected $title; 

    /** 
    * @ORM\Column(type="datetime") 
    * @Assert\NotBlank() 
    */ 
    protected $date; 
    //////ETC 

然後在phpMyAdmin:

 Action   Keyname Type Unique Packed Column Cardinality Collation Null Comment 
Edit Drop PRIMARY BTREE Yes No id 63 A 

我的主鍵。非空。

使用app_dev.php應用程序運行運行良好,我作爲被抑制的錯誤假設。

回答

0

試着用這個。它正在爲我工​​作

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

我會建議設置字段不能爲空。

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

在因爲你有表的任何情況下,你可以使用學說的CLI這是很方便的隨時生成你的實體地圖。

php [path-to-doctrine]/doctrine orm:convert-mapping --filter="TableNameInCamelCase" --namespace="[your-entity-namespace]" --force --from-database annotation [destination-path-of-entities] 

甚至有一個用於生成獲取者和設置者。

相關問題