Symfony2和Doctrine2:没有为实体“X”指定标识符/主键。每个实体必须有一个标识符/主键

时间:2022-03-14 06:48:07

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.

我正在使用Symfony2创建一些虚拟项目。我继续使用Symfony2图书文档来创建一个具有Doctrine命令行(http://symfony.com/doc/current/book/doctrine in.html #add- apping-information)的实体。因此,我使用的是注解,纯实体产品,没有关联。

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.

我已经复制了正确的示例:我成功地创建了数据库并创建了表产品。然后,我删除了所有内容,并尝试重新创建数据库(一切正常)和表(问题!),这只是为了测试目的。Doctrine不能生成更多的getter和setter,也不能在MySQL数据库上创建表。

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.

我的代码与Symfony2图书文档中的代码完全相同,只是包名更改为MyBundle。文件和文件夹的结构与其他特性一起正常工作。项目没有特定的设置,只有基本设置。

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
  • 在产品实体中只保留id(作为主键)
  • 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
  • 检查所有的double ** *以获得注释
  • 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
  • 我还执行了(感谢@Srdjan) php应用/控制台原则:cache:clear-metadata & app/console原则:cache:clear-query & app/console原则:cache:clear-result,但是getter和setter都没有生成
  • I've removed the table, database and recreated step by step (thanks @paistra)
  • 我已经删除了表、数据库并一步一步地重新创建(感谢@paistra)
  • I've tried to remove the table with

    我想把桌子搬走

    app/console doctrine:schema:drop

    app /控制台学说:模式:下降

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

谢谢你的帮助

4 个解决方案

#1


3  

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

如果您已经将映射从xml/yml转换为注解,请确保删除旧的映射文件。

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

在这个示例中,运行:php应用程序/控制台原则:mapping:convert annotation

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

转换:AppBundle \ \ config \ \ MyEntity.doctrine.xml教义的资源

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

将此文件内联到注释:AppBundle\Entity\MyEntity.php

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

由于在使用注释之前使用xml映射,所以会抛出错误。删除xml,应该没问题。

#2


0  

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


0  

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

在您的项目检查实体产品中,定义类似于示例(注释@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


0  

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;
}

#1


3  

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

如果您已经将映射从xml/yml转换为注解,请确保删除旧的映射文件。

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

在这个示例中,运行:php应用程序/控制台原则:mapping:convert annotation

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

转换:AppBundle \ \ config \ \ MyEntity.doctrine.xml教义的资源

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

将此文件内联到注释:AppBundle\Entity\MyEntity.php

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

由于在使用注释之前使用xml映射,所以会抛出错误。删除xml,应该没问题。

#2


0  

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


0  

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

在您的项目检查实体产品中,定义类似于示例(注释@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


0  

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;
}