【问题标题】:Symfony2 and Doctrine2: No identifier/primary key specified for Entity "X". Every Entity must have an identifier/primary keySymfony2 和 Doctrine2:没有为实体“X”指定标识符/主键。每个实体都必须有一个标识符/主键
【发布时间】:2015-01-27 10:35:23
【问题描述】:

我正在使用 Symfony2 创建一些虚拟项目。我坚持使用 Symfony2 Book 文档来使用 Doctrine 命令行 (http://symfony.com/doc/current/book/doctrine.html#add-mapping-information) 创建一个实体。因此,我使用的是注释、普通实体 Product 并且没有关联。

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

这里有一些输出

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[="..."]]

我的架构

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 安装包

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

我的代码

我的代码与 Symfony2 Book 文档中的代码完全相同,只是 Bundle 名称已更改为 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;
}

已经尝试过

  • 更改项目权限
  • 以 root 身份执行“应用程序/控制台原则:生成:实体”
  • 清除缓存
    • 执行 cache:clearcache:warmup
    • 手动删除缓存文件夹
  • 在 Product 实体中只保留 id(作为主键)
  • 删除并重新创建整个数据库
  • 删除产品实体,清除缓存并重新创建所有内容(手动)
  • 更新所有供应商
  • 检查了所有双**的注释
  • 更换具有相同软件架构的计算机
  • 我也执行了(感谢@Srdjan) php 应用程序/控制台原则:缓存:清除元数据 && 应用程序/控制台原则:缓存:清除查询 && 应用程序/控制台原则:缓存:清除结果 但不会生成 getter 和 setter
  • 我已删除表、数据库并逐步重新创建(感谢@paistra)
  • 我已经尝试删除表格

    app/console 原则:schema:drop

但错误总是一样的

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

即使表不存在..


感谢您的帮助

【问题讨论】:

  • 嗯,这不是一个答案,但我至少可以确认我和你在同一条船上......至少我知道我不是唯一的一个。唯一似乎不同的是我从现有的数据库中生成了实体。但是我进去后添加了id注释让它开心。
  • 我发现了我的问题。如果您有多个映射文件。在我的情况下AppBundle\Resources\config\doctrine\MyEntity.doctrine.xmlAppBundle\Entity\MyEntity.php 中的注释它将首先尝试使用xml。删除 xml 后(因为我想要注释)它工作正常。 (见下面的答案)

标签: php mysql symfony doctrine-orm symfony-2.6


【解决方案1】:

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

在这个例子中运行:php app/console doctrine:mapping:convert annotation

转换:AppBundle\Resources\config\doctrine\MyEntity.doctrine.xml

嵌入到此文件的注释中:AppBundle\Entity\MyEntity.php

抛出错误是因为它在使用注释之前使用了 xml 映射。 把xml删掉应该就好了。

【讨论】:

    【解决方案2】:

    如果您对实体类进行了更改,您还必须清除 Doctrine 缓存:

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

    【讨论】:

      【解决方案3】:

      在您的项目中检查实体产品的定义类似于示例(带有注释@ORM...)

      在进行新测试时也尝试删除表产品

      // 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】:

        是的,我的产品实体与您报告的完全一样

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

        【讨论】:

        • 你说你已经创建了`protuct table 尝试删除它
        • 我试过了,但令人惊讶的是,错误与“缺少主键”相同(即使表不存在)..
        • 转储数据库并使用 symfony 控制台构建它
        • 我做了但是没有用: 1. 我可以删除数据库 (app/console dictionary:database:drop --force) 2. 我可以重新创建数据库 (app/console dictionary:database :create) 3. 我可以执行实体生成命令,但没有为该实体生成 getter/setter(app/console 原则:generate:entities MyBundle)
        • 那么您对doctirne:shema:update --dump-sql 有什么看法。抱歉,但我不明白为什么 doctrine:generate:entities MyBundle 不起作用我用我的 IDE 生成 getter/setter。
        猜你喜欢
        • 2013-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-06
        • 1970-01-01
        • 1970-01-01
        • 2015-07-25
        • 2016-01-31
        相关资源
        最近更新 更多