Php Symfony2和Doctrine2:未为实体“指定标识符/主键”;X";。每个实体必须有一个标识符/主键
我正在使用Symfony2创建一些虚拟项目。我将使用Symfony2书籍文档创建一个带有命令行()的实体。因此,我使用的是注释,纯实体产品,没有关联 我复制了一个确切的例子:我成功地创建了数据库并创建了表产品。然后,我删除了所有内容,并尝试重新创建数据库(一切正常)和表(问题!),这只是出于测试目的。 Doctrine无法生成更多的getter和setter,也无法在MySQL数据库上创建表 这里有一些输出Php Symfony2和Doctrine2:未为实体“指定标识符/主键”;X";。每个实体必须有一个标识符/主键,php,mysql,symfony,doctrine-orm,symfony-2.6,Php,Mysql,Symfony,Doctrine Orm,Symfony 2.6,我正在使用Symfony2创建一些虚拟项目。我将使用Symfony2书籍文档创建一个带有命令行()的实体。因此,我使用的是注释,纯实体产品,没有关联 我复制了一个确切的例子:我成功地创建了数据库并创建了表产品。然后,我删除了所有内容,并尝试重新创建数据库(一切正常)和表(问题!),这只是出于测试目的。 Doctrine无法生成更多的getter和setter,也无法在MySQL数据库上创建表 这里有一些输出 app/console doctrine:generate:entities MyBun
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)
-
我的架构
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
我的代码
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
我的代码与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;
}
已尝试
- 更改项目权限
- 以root用户身份执行“应用程序/控制台原则:生成:实体”
- 清除缓存
- 执行缓存:清除和缓存:预热
- 手动删除缓存文件夹
- 在产品实体中只保留id(作为主键)
- 删除并重新创建整个数据库
- 删除产品实体,清除缓存并重新创建所有内容(手动)
- 更新所有供应商
- 检查注释的所有双**
- 用相同的软件体系结构更换计算机
- 我也执行了(谢谢@Srdjan) php应用程序/控制台原则:缓存:清除元数据和应用程序/控制台原则:缓存:清除查询和应用程序/控制台原则:缓存:清除结果 但不会生成getter和setter
- 我已经删除了表、数据库并一步一步地重新创建(谢谢@paistra)
- 我试着用手把桌子移走 应用程序/控制台原则:架构:删除
[Doctrine\ORM\Mapping\MappingException]
No identifier/primary key specified for Entity "MyBundle\Entity\Product".
Every Entity must have an identifier/primary key.
即使表格不存在
感谢您在项目中的帮助检查实体产品的定义与示例类似(带有注释@ORM…) 在执行新测试时也尝试删除表produt
// 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;
}
如果对实体类进行了更改,则还必须清除条令缓存:
php app/console doctrine:cache:clear-metadata && app/console doctrine:cache:clear-query && app/console doctrine:cache:clear-result
是的,我的产品实体与您报告的完全相同
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;
}
如果已将映射从xml/yml转换为注释,请确保删除旧的映射文件 在本例中,运行:
php应用程序/控制台原则:映射:转换注释
要转换:AppBundle\Resources\config\doctor\MyEntity.doctor.xml
与此文件内联的注释:AppBundle\Entity\MyEntity.php
引发错误,因为它在使用批注之前使用xml映射。
删除xml应该很好。您说您已经创建了“protuct表,尝试删除它我已经尝试过了,但令人惊讶的是错误与“主键丢失”相同(即使该表不存在)…转储数据库并使用symfony控制台构建它我这样做了,但没有任何效果:1。我可以删除数据库(应用程序/控制台条令:database:drop--force)2。我可以重新创建数据库(应用程序/控制台条令:数据库:创建)3。我可以执行实体生成命令,但是没有为该实体生成getter/setter(app/console原则:generate:entities-MyBundle),那么您使用
doctirne:shema:update--dump-sql有什么用呢。对不起,我不明白为什么我的IDE不能生成getter/setter。这不是一个答案,但我至少可以确认我和你在同一条船上。。。至少我知道我不是唯一一个。唯一不同的是,我已经从现有数据库生成了实体。但我进去后添加了Id注释,让它更开心。我发现了我的问题。如果您有多个映射文件。在我的例子中,AppBundle\Resources\config\doctor\MyEntity.doctor.xml
和AppBundle\Entity\MyEntity.php
中的注释将首先尝试使用xml。删除xml后(因为我需要注释),它工作得很好。(见下面的答案)