Symfony2 / Doctrine2无效的映射文件在尝试生成实体时出现异常

问题描述 投票:6回答:4

我有一个非常简单的数据库,我试图导入,并从中创建实体。 Doctrine(Symfony)能够从数据库生成YML映射文件。但是,当我临时尝试生成实体时,我收到以下错误:

[Doctrine\Common\Persistence\Mapping\MappingException]
Invalid mapping file 'SandboxBundle.Entity.Product.orm.yml' for class
'SandboxBundle\Entity\Product'.

yml文件对我来说很好,因为我们期望它是由Doctrine生成的。只是为了确定,我检查了一个在线yml验证器,它说没关系。我用来尝试生成实体的命令是:

app/console generate:doctrine:entities sandbox

.yml文件如下。请原谅这里粘贴文件导致的任何yml间距错误。正如我所说,yml文件是由doctrine生成的,并且确实通过了在线验证。

Product:
  type: entity
  table: product
    indexes:
      category_id:
          columns:
              - category_id
  id:
      id:
          type: integer
          nullable: false
          unsigned: false
          comment: ''
          id: true
          generator:
              strategy: IDENTITY
  fields:
      productname:
          type: string
          nullable: true
          length: 10
          fixed: false
          comment: ''
      categoryId:
          type: integer
          nullable: true
          unsigned: false
          comment: ''
          column: category_id
 lifecycleCallbacks: {  }

为了完整起见,这里是Category yml文件。错误发生在产品上,但我认为这是因为产品首先得到处理。

Category:
   type: entity
   table: category
       id:
          id:
              type: integer
              nullable: false
              unsigned: false
              comment: ''
              id: true
              generator:
                  strategy: IDENTITY
      fields:
          categoryname:
              type: string
              nullable: true
              length: 50
              fixed: false
              comment: ''
      lifecycleCallbacks: {  }

我在网上搜索了有关诊断映射例外的任何资源,但没有找到任何资源。我认为YML文件中存在导致实体生成器阻塞的内容。但错误消息没有说明可能是什么。我看到Stack Overflow上有很多关于这类问题的实例。获取有关如何诊断这些类型的错误的信息会非常棒,因此能够自己解决这个问题。

php symfony doctrine-orm mappingexception
4个回答
10
投票

已在doc中描述:

YAML文件中指定的类名应该是完全限定的。

因此,请尝试更改产品yaml定义,如下所示:

SandboxBundle\Entity\Product:
  type: entity
  table: product
    indexes:
  .....

在其他映射文件中执行相同的操作。

希望这有帮助


2
投票

尝试重命名该文件

SandboxBundle.Entity.Product.orm.yml

Product.orm.yml

并确保在yaml中完全符合您的姓名

SandboxBundle\Entity\Product:
type: entity
table: product
indexes:
.....

由于双重命名空间,您可能会收到错误。 Symfony将文件的虚线部分添加到命名空间。

[Doctrine\Common\Persistence\Mapping\MappingException]
Invalid mapping file 'SandboxBundle.Entity.SandboxBundle.Entity.Product.orm.yml' for class
'SandboxBundle\Entity\SandboxBundle\Entity\Product'.

祝好运 :)


1
投票

最近我使用以下代码遇到了类似的“MappingException:Invalid mapping file”异常:

//bootstrap.php
$config = Setup::createYAMLMetadataConfiguration(array(__DIR__ . 
"/../config/dcm"), $isDevMode);

使用Symfony Yaml 2. *,一切正常。但是使用Symfony Yaml ^ 3.3我会得到无效的映射文件异常。我追溯到不同的Yaml库解析函数是如何工作的。使用Doctrine解析yaml文件时,它将使用YamlDriver类,该类使用此函数加载yaml文件:

// vendor/doctrine/orm/lib/Doctrine/Orm/Mapping/Driver/YamlDriver.php
protected function loadMappingFile($file)
{
    return Yaml::parse($file);
}

在Yaml 2. *中,传递文件名字符串到解析工作没有问题,但是使用Yaml ^ 3.3,解析函数需要一个yaml字符串。解决此问题的一些方法包括使用xml配置文件或编写自己的Yaml驱动程序并绕过Setup :: createYAMLMetadataConfiguration并使用以下代码获取配置:

$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(new \MyNamespace\YamlDriver($paths));

return $config;

1
投票

使用

php app/console doctrine:generate:entity

通过学说自动生成实体。你用过了

app/console doctrine:generate:entities entityName

我的意思是

app/console generate:doctrine:entities entityName

(注意复数“实体”一词)您的命令是生成现有实体的更新属性并生成其getter和setter方法,但不生成实体。

我的建议是:

  1. 删除现有实体
  2. 使用适当的命令再次生成它们。
© www.soinside.com 2019 - 2024. All rights reserved.