我可以将Doctrine实体或其映射文件组织到子文件夹中吗? [英] Can I organize Doctrine entities or their mapping files into subfolders?

查看:66
本文介绍了我可以将Doctrine实体或其映射文件组织到子文件夹中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为映射文件尝试了以下目录结构:

I have tried this directory structure for my mapping files:

/config/doctrine/Place.orm.yml
/config/doctrine/Place/Type.orm.yml
/config/doctrine/Place/Price.orm.yml

并已指向我的映射文件中的相应实体,如下所示:

And have pointed to the corresponding Entity in my mapping file like this:

Project\TestBundle\Entity\Place\Type:
  type: entity
  table: place_type
  id:
    id:
      type: integer
      generator: { strategy:AUTO }
  fields:
    name:
      type: string
      length: 255

但这将返回错误。系统似乎无法检测到Entity的映射文件。

But this returns an error. The system can't seem to detect the mapping file for Entity.

推荐答案

总之,这是可能的。

如果捆绑包的Entity文件夹中有文件夹结构,这很简单。您必须使用实体名称空间下面的实体名称空间部分命名ORM文件,并将 \ 替换为

If you have folder structure inside your bundle's Entity folder, it's simple. You have to name your ORM files using entity namespace part below of Entity namespace and replacing \ with ..

例如,如果您有 Project\TestBundle\Entity\Place\Type 实体,ORM文件(位于捆绑包内的 config / doctrine 文件夹中)将具有名称 Place.Type.orm.yml

So if, for example, you have Project\TestBundle\Entity\Place\Type entity, the ORM file (located in config/doctrine folder inside bundle) will have name Place.Type.orm.yml.

如果要从Entity文件夹之外(甚至在bundle文件夹之外)用作Doctrine实体类,它会有点复杂,但仍然可能。 Doctrine Bundle允许在其配置中为您的类定义自定义映射位置。

If you want to use as Doctrine entities classes from outside of Entity folder (or even outside of the bundle folder), it gets a little bit complicated, but still possible. Doctrine Bundle allows to define custom mapping locations for your classes in its configuration.

再次-示例。如果您的实体位于 Project\Test 命名空间内(在文件夹 src / Project / Test 中),则可以定义这样的映射:

Again - example. If you have your entities inside of Project\Test namespace (in folder src/Project/Test), you can define mapping like this:


app / config / config * .yml

app/config/config*.yml



doctrine:
  orm:
    MyCustomDomain:
          mapping:              true
          type:                 yml
          dir:                  %kernel.root_dir%/config/projecttest
          alias:                ProjectTest
          prefix:               Project\Test
          is_bundle:            false

事实上,Doctrine Bundle自动执行类似的操作,这就是为什么您可以将所有类都放在Entity子文件夹中并且不再担心的原因。

In fact, Doctrine Bundle does something similar automatically, that's why you can put all of your classes in Entity subfolder and fear no more.

前缀是名称空间前缀。文件夹是包含配置文件的文件夹的路径。别名很有趣-它允许在DQL查询和映射文件中使用更简单的对象名称。 Symfony的 TestBundle:Test 语法在同一前提下工作- TestBundle 是TestBundle中所有实体的别名。 is_bundle 告诉Doctrine,这些实体不在Symfony捆绑包中,需要一些不同的处理。

The prefix is namespace prefix. Folder is the path to folder with configuration files. Alias is interesting - it allows to use simpler object names in DQL queries and mapping files. Symfony's TestBundle:Test syntax works on the same premise - TestBundleis the alias for all entities in TestBundle. is_bundle tells Doctrine, that the entities are outside of Symfony bundle and require a little bit different treatment.

有在定义您自己的映射时的一些注意事项。 Mapper使用前缀上的首次匹配规则。因此,如果在过于宽泛的名称空间前缀上声明了映射,则它可以覆盖其他映射。

There are some caveats in defining your own mapping. Mapper works using 'first match' rule on prefix. So if you declare your mapping on too broad namespace prefix, it can override other mappings.

尽管如此,有时它还是有用的。例如,如果您想将类从外国库直接映射到Doctrine。或者正在创建不完全与Symfony绑定的库,并希望将某些类保留在捆绑包之外。

Nevertheless, it is useful sometimes. For example, if you want to map classes from "foreign" library directly to Doctrine. Or are creating a library not completely tied to Symfony and want to keep some of your classes outside of the bundle.

这篇关于我可以将Doctrine实体或其映射文件组织到子文件夹中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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