3

我正在使用 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 身份执行“应用程序/控制台原则:生成:实体”
  • 清除缓存
    • 执行缓存:清除缓存:预热
    • 手动删除缓存文件夹
  • 在 Product 实体中只保留 id(作为主键)
  • 删除并重新创建整个数据库
  • 删除产品实体,清除缓存并重新创建所有内容(手动)
  • 更新所有供应商
  • 检查了所有双**的注释
  • 更换具有相同软件架构的计算机
  • 我还执行了(感谢@Srdjan) php app/console 学说:cache:clear-metadata && app/console 学说:cache:clear-query && app/console 学说:cache:clear-result 但不会生成 getter 和 setter
  • 我已逐步删除表、数据库并重新创建(感谢@paistra)
  • 我试图删除表格

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

但错误总是一样的

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

即使表不存在..


谢谢您的帮助

4

4 回答 4

3

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

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

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

进入与此文件内联的注释:AppBundle\Entity\MyEntity.php

抛出错误是因为它在使用注释之前使用了 xml 映射。删除xml,应该没问题。

于 2016-02-10T14:11:51.233 回答
0

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

php app/console doctrine:cache:clear-metadata && app/console doctrine:cache:clear-query && app/console doctrine:cache:clear-result
于 2015-01-27T10:47:57.363 回答
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;
}
于 2015-01-27T10:48:28.620 回答
0

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

在进行新测试时也尝试 drop table 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;
}
于 2015-01-27T10:38:22.423 回答