Pregunta

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?

¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top