views:

365

answers:

3

if i do integrate Zend Framework 1.10 with Doctrine 2 where do i put my Doctrine Models/Entities and Proxies? i thought of the /application or the /library directories. if i do put in the /library directory tho, will it interfere with ZF autoloading classes from there since the classes there will be using PHP 5.3 namespaces vs PEAR style namespaces.

A: 

I would put the Models in the same directory where the "normal" Zend Framework models life. /models

You can tell Doctrine to generate the models at this place, and prefix them with "Default_Model" or whatever.

Check out one of John Lebenshold Screencasts about Zend Framework and Doctrine

Zend Screencasts

ArneRie
jiewmeng
+1  A: 

In theory, you could put then anywhere, as long as the namespaces resolve correct.

I would suggest this structure:

/application/models/MyApp/Entities
/application/models/MyApp/Proxies

Load the 'MyApp' using Doctrine's ClassLoader. I've had no conflicts using the Doctrine loader with the Zend Loader (if you have classes that use the PEAR convention inside your namespace folder, you will still need to use the Zend Loader).

Remember that 'models' can be more than just your Entity classes. My model layer consists of interfaces, factories, validators and service objects. To that end, anything that is application specific business logic should probably go in the model folder.

Bryan M.
how do u configure Doctrine class loader to work with zend autoloader? i am currently placing my Entities into the `/library/Application` folder. then in application.ini added `autoloaderNamespaces[] = Application`. it works fine as Zend Loader seems to be able to work with PHP 5.3 namespaces, however i cant get it to load classes from somewhere outside the `include_path`s. it will be good to know how i can do this with Doctrine class loader and Zend (specifying a custom location to find files)
jiewmeng
See this section of the manual: http://www.doctrine-project.org/projects/orm/2.0/docs/reference/configuration/en#bootstrapping:class-loading:git Basically, you can use Doctrine's ClassLoader to load any PHP 5.3 namespace. Just provide the name of the base namespace (like 'Doctrine' or 'MyApp', and a path to the folder. You can then import any namespace classes with the 'use' keyword.
Bryan M.
i know, my question now is how do i add the Doctrine class loader as a class loader to my Zend Application?
jiewmeng
I run the Doctrine class loader in the index.php of my app
Bryan M.
any code? i am not sure how the code will look like?
jiewmeng
+3  A: 

I'm working on an application that integrates Doctrine 2 with ZF1.10 also.You don't need to use the Doctrine auto loader at all.

1) In your application.ini file add the following line (assuming you have Doctrine installed in your library folder (same as the Zend folder):

autoloadernamespaces.doctrine = "Doctrine"

2) Create a doctrine or entitymanager resource. In your ini file:

resources.entitymanager.db.driver = "pdo_mysql"
resources.entitymanager.db.user = "user"
resources.entitymanager.db.dbname = "db"
resources.entitymanager.db.host = "localhost"
resources.entitymanager.db.password = "pass"
resources.entitymanager.query.cache = "Doctrine\Common\Cache\ApcCache"
resources.entitymanager.metadata.cache = "Doctrine\Common\Cache\ApcCache"
resources.entitymanager.metadata.driver = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.entitymanager.metadata.proxyDir = APPLICATION_PATH "/../data/proxies"
resources.entitymanager.metadata.entityDir[] = APPLICATION_PATH "/models/entity"

3) Next, you will need to bootstrap it. I added a resource class in my resources folder. Make sure you map to the folder in your ini file:

pluginPaths.Application_Resource_ = APPLICATION_PATH "/resources"

Then your resource class...

class Application_Resource_EntityManager 
extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * @var Doctrine\ORM\EntityManager
     */
    protected $_em;

    public function init()
    {
        $this->_em = $this->getEntityManager();
        return $this->_em;
    }

    public function getEntityManager()
    {
        $options = $this->getOptions(); 
        $config = new \Doctrine\ORM\Configuration();
        $config->setProxyDir($options['metadata']['proxyDir']);
        $config->setProxyNamespace('Proxy');
        $config->setAutoGenerateProxyClasses((APPLICATION_ENV == 'development'));
        $driverImpl = $config->newDefaultAnnotationDriver($options['metadata']['entityDir']);
        $config->setMetadataDriverImpl($driverImpl);
        $cache = new Doctrine\Common\Cache\ArrayCache();
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);

        $evm = new Doctrine\Common\EventManager();
        $em = Doctrine\ORM\EntityManager::create($options['db'],$config,$evm);

        return $em;
    }
}

The doctrine 2 entity manager is now available to your application. In your controller you can grab it like so:

$bootstrap = $this->getInvokeArg('bootstrap');
$em = $bootstrap->getResource('entitymanager');

I am sure this will help somebody :)

David