Symfony2 中使用 YAML 的 Doctrine 映射 [英] Doctrine Mapping in Symfony2 using YAML

查看:29
本文介绍了Symfony2 中使用 YAML 的 Doctrine 映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Symfony2 中 Doctrine 的 YAML 配置的问题.

I have a question regarding YAML configuration of Doctrine in Symfony2.

我通过doctrine:generate:entity"创建了一个实体,并选择了YAML作为映射格式.这没有在 ../Entity/"MyEntity".php 上添加任何元数据,这将允许我更新或创建我的架构.

I have created an entity via "doctrine:generate:entity", and chose YAML as the mapping format. This didn't add any metadata on ../Entity/"MyEntity".php, which would allow me to update or create my schema.

举个例子,如果我运行

./app/console doctrine:schema:create

它失败了,说:

[RuntimeException]                                                 
Bundle "MySuperBundle" does not contain any mapped entities.

我的自动映射已设置为true".

My automapping is already set to "true".

如果我选择使用注解配置,这将不是问题.

If I choose to use annotation config this would not be a problem.

我错过了什么吗?有什么我应该采取的额外步骤吗?

Did I miss something? Are there any extra steps that I should take?

预先感谢您,问候,

伊万

推荐答案

我刚刚在看 Doctrine 配置初始化代码时玩得很开心.我发现的是:

I just had a fun time looking at the Doctrine config initialisation code. What I found was:

  • 使用 auto_mapping 会导致为单个默认实体管理器设置各种默认值;它将 type 值保留为 false
  • 如果 type 为假,配置代码会在默认目录中查找可能的配置文件,一旦找到一个有效扩展名的文件,它就会决定这就是配置的方式,按照xmlymlphp
  • 的顺序
  • 如果它没有找到任何一个,它会假设 annotation
  • Using auto_mapping results in various defaults being set for the single default entity manager; it leaves the type value as false
  • If type is false the config code looks into the default directory for likely config files, and as soon as it finds a file of a valid extension it decides that that's the way config is being done, in the order xml, yml, php
  • If it doesn't find any of those it assumes annotation

您在 Bundle/Resources/config/doctrine 文件夹中还有其他任何东西吗?如果是这样,它可能会放弃自动检测.

Do you have anything else at all in the Bundle/Resources/config/doctrine folder? If so, it might be throwing off the auto-detection.

除此之外,基本上如果你使用了默认值,并且有一些实体类和有效的配置,你所做的应该可以在没有任何额外配置的情况下工作.您已经说过auto_mapping"是正确的,但是您是否更改了 Doctrine 配置的任何其他部分?

That aside, basically if you've used defaults, and have some entity classes and valid config, what you're doing should be working without any additional config. You've said "auto_mapping" is true, but have you changed any other bit of Doctrine config?

尝试显式配置内容可能是一个想法,例如如 Symfony Doctrine 文档 中所述,从默认配置开始

It might be an idea to try configuring stuff explicitly, e.g. as described in the Symfony Doctrine docs, go from default config

doctrine:
    dbal:
        driver:   "%database_driver%"
        #etc

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

显式

doctrine:
    dbal:
        driver:   "%database_driver%"
        #etc

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: false
        mappings:
            MySuperBundleName:
                type: yml
                dir: Resources/config/doctrine

这篇关于Symfony2 中使用 YAML 的 Doctrine 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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