Symfony2 and Doctrine2: No identifier/primary key specified for Entity “X”. Every Entity must have an identifier/primary key

[亡魂溺海] 提交于 2019-12-12 16:22:17

问题


I'm using Symfony2 to create some dummy projects. I'm keeping to the Symfony2 Book documentation to create an Entity with Doctrine command line (http://symfony.com/doc/current/book/doctrine.html#add-mapping-information). I'm using, thus, annotations, the plain entity Product and no associations.

I've copied the exact example: I've successfully created the database and created the table Product. Then, I've removed everything and tried to recreate the database (everything's fine) and the table (problem!), that just for testing purposes. Doctrine can't generate any more getters and setters and can't create the table on the MySQL database.

Here some output

app/console doctrine:generate:entities MyBundle

> backing up Product.php to Product.php~
> generating MyBundle\Entity\Product

(nothing happens except for creating the identical backup file with no setters / getters added)

-

app/console doctrine:schema:validate
OR
app/console doctrine:schema:create
OR
app/console doctrine:schema:update --force
----------
[Doctrine\ORM\Mapping\MappingException]
No identifier/primary key specified for Entity "MyBundle\Entity\Product".
Every Entity must have an identifier/primary key.

doctrine:schema:validate [--em[="..."]]

My Architecture

PHP          5.5.21 (tried also with PHP 5.5.14)
Apache       2.4.9
Mac OSX      10.10.1 - Yosemite
MySQL        5
ENV          dev

Composer installed packages

doctrine/annotations                 v1.2.3  Docblock Annotations Parser
doctrine/cache                       v1.4.0  Caching library offering an object-oriented API for many cache backends
doctrine/collections                 v1.2    Collections Abstraction library
doctrine/common                      v2.4.2  Common Library for Doctrine projects
doctrine/dbal                        v2.5.1  Database Abstraction Layer
doctrine/doctrine-bundle             v1.3.0  Symfony DoctrineBundle
doctrine/doctrine-cache-bundle       v1.0.1  Symfony2 Bundle for Doctrine Cache
doctrine/inflector                   v1.0.1  Common String Manipulations with regard to casing and singular/plural rules.
doctrine/lexer                       v1.0.1  Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm                         v2.4.7  Object-Relational-Mapper for PHP
incenteev/composer-parameter-handler v2.1.0  Composer script handling your ignored parameter file
jdorn/sql-formatter                  v1.2.17 a PHP SQL highlighting library
kriswallsmith/assetic                v1.2.1  Asset Management for PHP
monolog/monolog                      1.12.0  Sends your logs to files, sockets, inboxes, databases and various web services
psr/log                              1.0.0   Common interface for logging libraries
sensio/distribution-bundle           v3.0.15 Base bundle for Symfony Distributions
sensio/framework-extra-bundle        v3.0.4  This bundle provides a way to configure your controllers with annotations
sensio/generator-bundle              v2.5.1  This bundle generates code for you
sensiolabs/security-checker          v2.0.1  A security checker for your composer.lock
swiftmailer/swiftmailer              v5.3.1  Swiftmailer, free feature-rich PHP mailer
symfony/assetic-bundle               v2.6.0  Integrates Assetic into Symfony2
symfony/monolog-bundle               v2.7.1  Symfony MonologBundle
symfony/swiftmailer-bundle           v2.3.8  Symfony SwiftmailerBundle
symfony/symfony                      v2.6.3  The Symfony PHP framework
twig/extensions                      v1.2.0  Common additional features for Twig that do not directly belong in core
twig/twig                            v1.18.0 Twig, the flexible, fast, and secure template language for PHP

My Code

My code is exactly as in the Symfony2 Book documentation except for the Bundle name, changed to MyBundle. The structure of files and folders is working properly with other features. There are no particular settings for the project, just the basics.

namespace MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;

    /**
     * @ORM\Column(type="decimal", scale=2)
     */
    protected $price;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;
}

Already tried to

  • Change project permissions
  • Execute "app/console doctrine:generate:entities" as root
  • Clear the cache
    • Execute cache:clear and cache:warmup
    • Manually remove the cache folder
  • Keep just the id (as primary key) in the Product entity
  • Remove and recreate the whole database
  • Remove Product Entity, clear the cache and recreate everything (manually)
  • Update all the vendors
  • Checked all the double ** for the annotations
  • Change computer with the same software architecture
  • I've also executed (thanks @Srdjan) php app/console doctrine:cache:clear-metadata && app/console doctrine:cache:clear-query && app/console doctrine:cache:clear-result but getters and setters are not generated
  • I've removed the table, database and recreated step by step (thanks @paistra)
  • I've tried to remove the table with

    app/console doctrine:schema:drop

but the error is always the same

[Doctrine\ORM\Mapping\MappingException]
No identifier/primary key specified for Entity "MyBundle\Entity\Product".
Every Entity must have an identifier/primary key.

even if the table doesn't exist..


Thank you for your help


回答1:


If you have converted your mappings into annotations from xml/yml be sure to delete the old mappings files.

In this example running: php app/console doctrine:mapping:convert annotation

to convert: AppBundle\Resources\config\doctrine\MyEntity.doctrine.xml

into annotations inline with this file: AppBundle\Entity\MyEntity.php

error is thrown because its using xml mappings before it uses annotations. Delete the xml and should be well.




回答2:


If you made changes on entity class you must to clear Doctrine cache also:

php app/console doctrine:cache:clear-metadata && app/console doctrine:cache:clear-query && app/console doctrine:cache:clear-result



回答3:


In your project check entity Product is defined like the exemple (with the comment @ORM...)

try drop table produt too when do a new test

// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;

    /**
     * @ORM\Column(type="decimal", scale=2)
     */
    protected $price;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;
}



回答4:


yes, my Product Entity is exactly like the one you reported

namespace MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;

    /**
     * @ORM\Column(type="decimal", scale=2)
     */
    protected $price;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;
}


来源:https://stackoverflow.com/questions/28168364/symfony2-and-doctrine2-no-identifier-primary-key-specified-for-entity-x-ever

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!