How to generate Proxies manually without CLI in Doctrine?
Asked Answered
L

1

7

I'm using Zend Framework to create a web application. Based on several recommendations, I chose Doctrine as my RDBM system.

;---------------------------------------------------
; DOCTRINE CONFIGURATION
;---------------------------------------------------
resources.entityManager.connection.driver = "pdo_mysql"
resources.entityManager.connection.host = "localhost"
resources.entityManager.connection.dbname = "private"
resources.entityManager.connection.user = "private"
resources.entityManager.connection.password = "private"
resources.entityManager.connection.entities = APPLICATION_PATH "/models"
resources.entityManager.connection.proxies.location = APPLICATION_PATH "/models/Proxies"
resources.entityManager.connection.proxies.ns = "Proxies"

; According to Doctrine manual, this should be true for 
; development, and false for production
resources.entityManager.connection.proxies.generate = true

The above is my Doctrine config in the Zend application.ini. Everything is working fine, but I wanted to know in advance how to generate Proxies manually without using the CLI for several reasons. First of all, the Doctrine 2.0 doc mentions that auto-generating Proxies will cause performance issues. Second of all, I still haven't figured out how to use the Doctrine CLI especially that I've moved my project development to a shared server box without command prompt access.

I've been generating Doctrine entities manually by creating the classes. How do I manually generate Doctrine proxies similarly?

Lindsy answered 19/2, 2012 at 23:37 Comment(0)
T
8

I've found easy way to generate proxies:

    $proxyDir = null; //to genearate to default proxy dir
    $proxyFactory = $em->getProxyFactory();
    $metadatas = $em->getMetadataFactory()->getAllMetadata();
    $proxyFactory->generateProxyClasses($metadatas, $proxyDir);

to generate entities use:

    $classes = $em->getClassMetadataFactory()->getAllMetadata();
    $generator = new \Doctrine\ORM\Tools\EntityGenerator(); 
    $generator->setGenerateAnnotations(true); 
    $generator->setGenerateStubMethods(true); 
    $generator->setRegenerateEntityIfExists(false); 
    $generator->setUpdateEntityIfExists(true); 
    $generator->generate($classes, '/path/to/generate/entities');
Tyrr answered 8/5, 2012 at 22:2 Comment(2)
We have entities, however the third line of your code (the $metadatas line) is giving us errors: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'File mapping drivers must have a valid directory path, however the given path seems to be incorrect!' in C:\Zend\Apache2\htdocs\Webate\library\Doctrine\ORM\Mapping\MappingException.php:193Chenopod
u need to set proxy path in EntityManager, here code from my old project: $config = new \Doctrine\ORM\Configuration; $config->setMetadataCacheImpl(self::getCache()); $config->setMetadataDriverImpl(new \qweb\driver\DoctrineYml( QWEB_MODULE )); $config->setEntityNamespaces(array('base')); $config->setProxyDir(DOCTRINE_PROXIES); $config->setProxyNamespace('proxy'); $connection = \Doctrine\DBAL\DriverManager::getConnection(self::loadYml(QWEB_CONFIG.DIRECTORY_SEPARATOR.'databases.yml')); $em = \Doctrine\ORM\EntityManager::create($connection, $config);Procter

© 2022 - 2024 — McMap. All rights reserved.