Symfony2 Doctrine - 每个实体必须有一个标识符/主键 - 它有一个主键吗?

时间:2013-01-31 16:18:06

标签: php symfony doctrine

与SF2和Doctrine有一个奇怪的问题。当我试图从app.php运行我的应用程序时,我得到了这个:

[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   

我有主键。非是NULL。

使用app_dev.php运行应用运行正常,我假设错误被抑制。

2 个答案:

答案 0 :(得分:0)

尝试使用此功能。这对我有用

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

答案 1 :(得分:0)

我建议将字段设置为不可为空。

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

在任何情况下,由于您拥有该表,您始终可以使用doctrine的cli生成实体地图,这非常方便。

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

甚至还有一个用于生成getter和setter。