Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating ZF/Doctrine2: Where do i put my Models/Entities & Proxy classes

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.

like image 846
Jiew Meng Avatar asked Jul 24 '10 12:07

Jiew Meng


2 Answers

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 :)

like image 155
David Avatar answered Sep 19 '22 17:09

David


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.

like image 33
Bryan M. Avatar answered Sep 18 '22 17:09

Bryan M.