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

时间:2015-01-27 10:35:24

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

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

我已经复制了确切的示例:我已成功创建数据库并创建了表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;
}

已尝试

  • 更改项目权限
  • 执行" app / console doctrine:generate:entities"以root身份
  • 清除缓存
    • 执行缓存:清除缓存:预热
    • 手动删除缓存文件夹
  • 仅保留产品实体中的ID(作为主键)
  • 删除并重新创建整个数据库
  • 删除产品实体,清除缓存并重新创建所有内容(手动)
  • 更新所有供应商
  • 检查注释的所有双**
  • 使用相同的软件架构更改计算机
  • 我也被执行了(感谢@Srdjan) php app / console doctrine:cache:clear-metadata&& app / console doctrine:cache:clear-query&& app / console doctrine:cache:clear-result 但是没有生成getter和setter
  • 我已经删除了表格,数据库并逐步重新创建(感谢@paistra)
  • 我试图用

    删除表格

    app / console doctrine:schema:drop

但错误始终相同

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

即使表格不存在..


感谢您的帮助

4 个答案:

答案 0 :(得分:3)

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

在此示例中运行:AppBundle\Resources\config\doctrine\MyEntity.doctrine.xml

转换:AppBundle\Entity\MyEntity.php

与此文件内联的注释:gmail.com

抛出

错误是因为它在使用注释之前使用xml映射。 删除xml,应该很好。

答案 1 :(得分:0)

在项目检查实体中,产品的定义与示例相同(注释@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;
}

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

是的,我的产品实体与您报告的实体完全相同

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