Symfony2和Doctrine2:没有为实体“X”指定的标识符/主键。每个实体必须有一个标识符/主键 [英] Symfony2 and Doctrine2: No identifier/primary key specified for Entity "X". Every Entity must have an identifier/primary key

查看:330
本文介绍了Symfony2和Doctrine2:没有为实体“X”指定的标识符/主键。每个实体必须有一个标识符/主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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 

>将Product.php备份到Product.php〜
>生成MyBundle\Entity\Product

(除了创建没有setter / getter的相同的备份文件,没有任何反应)

-

  app / console doctrine:schema:validate 
OR
app / console doctrine:schema:create
OR
app / console doctrine:schema:update --force
----------
[Doctrine\ORM\Mapping\MappingException]
没有为实体MyBundle\Entity\Product指定的标识符/主键。
每个实体必须有一个标识符/主键。

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

我的架构

  PHP 5.5.21(尝试使用PHP 5.5.14 )
Apache 2.4.9
Mac OSX 10.10.1 - 优胜美地
MySQL 5
ENV dev

Composer安装的软件包

  doctrine / annotations v1。 2.3 Docblock注释解析器
doctrine / cache v1.4.0缓存库为多个缓存后端提供面向对象的API
doctrine / collections v1.2集合抽象库
doctrine / common v2.4.2常见图书馆的学说项目
doctrine / dbal v2.5.1数据库抽象层
doctrine / doctrine-bundle v1.3.0 Symfony DoctrineBundle
doctrine / doctrine-cache-bundle v1.0.1 Symfony2 Bundle for Doctrine Cach e
doctrine / inflector v1.0.1关于套管和单数/复数规则的常用字符串操作。
doctrine / lexer v1.0.1可以在自上而下,递归下降解析器中使用的词法分析器的基本库。
doctrine / orm v2.4.7 PHP的对象关系映射器
incenteev / composer-parameter-handler v2.1.0 Composer脚本处理您忽略的参数文件
jdorn / sql-formatter v1.2.17 PHP SQL突出显示库
kriswallsmith / assetic v1.2.1 PHP资产管理
monolog / monolog 1.12.0将日志发送到文件,套接字,收件箱,数据库和各种Web服务
psr / log 1.0.0日志记录库的通用界面
sensio / distribution-bundle v3.0.15 Symfony发行版的基础包
sensio / framework-extra-bundle v3.0.4此软件包提供了一种配置控制器的方法注释
sensio / generator-bundle v2.5.1此软件包为您生成代码
sensiolabs / security-checker v2.0.1您的composer.lock
swiftmailer / swiftmailer v5.3.1 S的安全检查程序wiftmailer,免费功能丰富的PHP邮件程序
symfony / assetic-bundle v2.6.0将Assetic集成到Symfony2
symfony / monolog-bundle v2.7.1 Symfony MonologBu​​ndle
symfony / swiftmailer-bundle v2.3.8 Symfony SwiftmailerBundle
symfony / symfony v2.6.3 Symfony PHP框架
twig / extensions v1.2.0不直接属于核心的Twig的常见附加功能
twig / twig v1.18.0 Twig, PHP
的灵活,快速和安全的模板语言

我的代码 / p>

我的代码与Symfony2 Book文档中的一样,除了Bundle名称,更改为MyBundle。文件和文件夹的结构与其他功能正常工作。项目没有特别的设置,只是基础知识。

 命名空间MyBundle\Entity; 

使用Doctrine\ORM\Mapping作为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:实体作为根

  • 清除缓存

    • 执行缓存:清除和缓存:预热

    • 手动删除缓存文件夹


  • 关键)

  • 删除并重新创建整个数据库

  • 删除产品实体,清除缓存并重新创建所有内容(手动)

  • 更新所有供应商

  • 检查所有双重**的注释

  • 使用相同的软件架构更改计算机

  • 我也执行了(感谢@Srdjan)
    php app / console doctrine:cache:clear-metadata&& app / console doctrine:cache:clear-query&&& app / console doctrine:cache:clear-result
    但是getter和setter不生成

  • 我删除了表,数据库并重新创建了一步一步(感谢@paistra)

  • 我试图用



    应用程序/控制台原则:schema:drop




但错误总是相同的

  [Doctrine\ORM\Mapping\MappingException] 
没有为EntityMyBundle\Entity\Product指定的标识符/主键。
每个实体必须有一个标识符/主键。

即使表不存在..






感谢您的帮助

解决方案

如果您已经转换映射到xml / yml的注释,一定要删除旧的映射文件。



在这个例子中运行: php app / console doctrine:mapping:转换注释



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



与此文件内嵌的注释: AppBundle\Entity\MyEntity.php



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


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

解决方案

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.

这篇关于Symfony2和Doctrine2:没有为实体“X”指定的标识符/主键。每个实体必须有一个标识符/主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆