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.
问题:
回答1:
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 :)
回答2:
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.
回答3:
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