Doctrine2 实体命名空间 [英] Doctrine2 Entity Namespace

查看:28
本文介绍了Doctrine2 实体命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Doctrine2 的新手,想知道如何告诉 Doctrine 我的实体使用哪个命名空间.我当前的配置是这个.

i'm new to Doctrine2 and like to know how i can tell Doctrine which namespace my entities use. My current configuration is this.

我所有的实体都在命名空间project\entity"中.所以,每次我想获得实体颜色"时,我必须写:

All my entities are in namespace "project\entity". So, everytime i want to obtain the entity "Color", i have to write:

$em->getRepository("project\\entity\\Color")

如何配置 Doctrine 以始终使用命名空间project\entity"?

How can i configure Doctrine to always use namespace "project\entity"?

推荐答案

您可以通过在配置对象上使用 addEntityNamespace 创建命名空间别名来接近您想要的:

You can come close to what you want by using addEntityNamespace on your config object to create a namespace alias:

$em->getConfiguration()->addEntityNamespace('NS1', 'Project\Entity');

$colorRepo = $em->getRepository('NS1:Color');

也适用于查询.

顺便说一下,"project\\entity\\Color" 也可以写成'project\entity\Color'.我还建议将 Project 和 Entity 大写以符合标准.

By the way, "project\\entity\\Color" can also be written as 'project\entity\Color'. I would also suggest capitalizing Project and Entity just to conform to standards.

这篇关于Doctrine2 实体命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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