原则 2 自动加载

发布于 2024-12-14 13:36:28 字数 518 浏览 2 评论 0原文

如何使用 Doctrine 2 和 Zend 设置自动加载以加载以下目录结构中的实体:

Application
-Modules
--Core
---Models
----Entities
----Repositories
--CMS
---Models
----Entities
----Repositories

我希望能够使用 {ModuleName}\Entities{EntityName} 加载类。例如,我希望能够执行此操作来加载“用户”实体:

$em->getRepository('Core\Entities\User');

或者对于“页面”实体执行类似的操作:

$em->getRepository('CMS\Entities\Pages');

我可以将其设置为加载“CMS\Models\Entities\Pages”,但我希望能够知道如何做到这一点,而不必直接映射到目录结构。这可能吗?

How do set up autoloading with Doctrine 2 and Zend to load entities in the following directory structure:

Application
-Modules
--Core
---Models
----Entities
----Repositories
--CMS
---Models
----Entities
----Repositories

I want to be able to load classes using {ModuleName}\Entities{EntityName}. For example, I'd like to be able to do this to load a 'User' entity:

$em->getRepository('Core\Entities\User');

or something like this for a 'Pages' entity:

$em->getRepository('CMS\Entities\Pages');

I can set it up to load 'CMS\Models\Entities\Pages' but I'd like to be able to know how to do it without having to map directly to the directory structure. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱ヾ落雨殇 2024-12-21 13:36:28

我不知道如何将 Zend Framework 和 Doctrine2 粘合在一起,但如果您使用流行的 Bisna 胶水(这非常酷),您可以在 application.ini 中设置多个映射目录。仔细查看以下 ini 设置:

resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.adapterClass          = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingNamespace      = "Core\Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingDirs[]         = APPLICATION_PATH "/modules/Core/Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderCache = default

resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.adapterClass          = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.mappingNamespace      = "CMS\Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.mappingDirs[]         = APPLICATION_PATH "/modules/CMS/Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.annotationReaderCache = default

类似上面的内容就可以实现您想要的。如果希望能够完全自动执行此操作,我认为您必须修补 Bisna\Doctrine\Container 类。例如,查看定义的模块,检查是否存在实体目录并将其添加到学说实体管理器中。

比斯纳
如果您不知道 Bisna 是什么,这是一个小型库,可让您轻松地将 Doctrine2 和 Zend Framework 1 “粘合”在一起。

通过观看此视频,您应该很容易了解如何集成 Doctrine2。
http://www.zendcasts.com/unit-testing- Doctrine-2-entities/2011/02/

请注意,视频中使用的 Bisna 版本仅支持 Doctrine 2.0,而不支持 2.1,在这种情况下,您应该使用此版本一:https://github.com/guilhermeblanco/ZendFramework1-Doctrine2

I don't how you glue Zend Framework and Doctrine2 together but if you are using the popular Bisna glue (which is pretty cool) you can set-up more than one mapping directory in your application.ini. Take a closer look to the following ini settings:

resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.adapterClass          = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingNamespace      = "Core\Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingDirs[]         = APPLICATION_PATH "/modules/Core/Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderCache = default

resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.adapterClass          = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.mappingNamespace      = "CMS\Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.mappingDirs[]         = APPLICATION_PATH "/modules/CMS/Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.annotationReaderCache = default

Something like the above would be accomplish what you want. If want to be able to do this fully automatically I think you have to patch the Bisna\Doctrine\Container class. Which for instance looks to the modules defined check if there is a entities directory and add's this to the doctrine entity manager.

Bisna
If you don't have a clue what Bisna is, this is a small library which allows you to easily 'glue' Doctrine2 and Zend Framework 1 together.

By watching this video it should be easy for you to understand how to integrate Doctrine2.
http://www.zendcasts.com/unit-testing-doctrine-2-entities/2011/02/

Please be aware that the Bisna version used in the video only supports Doctrine 2.0 and not 2.1 in that case you should use this one: https://github.com/guilhermeblanco/ZendFramework1-Doctrine2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文