Is this the right way to integrate Zend Framework with Doctrine 2?

I seem to be able to integrate Doctrine 2 autoloaders into Zend, but I'm not sure if I am doing this correctly ...

Structure

/application
    /models
        /User.php   // following classes are doctrine models
        /Post.php
        /Tag.php
    /proxies
        /...        // proxy classes generated by doctrine
    /...            // other zend classes

bootstrap.php> _initDoctrine()

// setup Zend & Doctrine Autoloaders
require_once "Doctrine/Common/ClassLoader.php";

$zendAutoloader = Zend_Loader_Autoloader::getInstance();

// $autoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass');

$autoloader = array(new \Doctrine\Common\ClassLoader('Symfony'), 'loadClass');
$zendAutoloader->pushAutoloader($autoloader, 'Symfony\\');
$autoloader = array(new \Doctrine\Common\ClassLoader('Doctrine'), 'loadClass');
$zendAutoloader->pushAutoloader($autoloader, 'Doctrine\\');
$autoloader = array(new \Doctrine\Common\ClassLoader('DoctrineExtensions'), 'loadClass');
$zendAutoloader->pushAutoloader($autoloader, 'DoctrineExtensions\\');
$autoloader = array(new \Doctrine\Common\ClassLoader('Application\\Models', realpath(__DIR__ . '/..')), 'loadClass');
$zendAutoloader->pushAutoloader($autoloader, 'Application\\Models\\');
$autoloader = array(new \Doctrine\Common\ClassLoader('Application\\Proxies', realpath(__DIR__ . '/..')), 'loadClass');
$zendAutoloader->pushAutoloader($autoloader, 'Application\\Proxies');
$autoloader = array(new \Doctrine\Common\ClassLoader('DoctrineExtensions'), 'loadClass');
$zendAutoloader->pushAutoloader($autoloader, 'DoctrineExtensions\\');

// setup configuration as seen from the sandbox application
// TODO: read configuration from application.ini
$config = new \Doctrine\ORM\Configuration;
$cache = new \Doctrine\Common\Cache\ArrayCache;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(realpath(__DIR__ . '/models'));
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setProxyDir(realpath(__DIR__ . '/proxies'));
$config->setProxyNamespace('Application\\Proxies');
$config->setAutoGenerateProxyClasses(true);

$connectionOptions = array(
  'driver' => 'pdo_mysql',
  'user' => 'root',
  'password' => '',
  'dbname' => 'learningzf'
);

// setup entity manager
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
Zend_Registry::set("em", $em);
return $em;

I have 1 question, can I use relative paths in newDefaultAnnotationDriver()and is it setProxyDir()safer to say that I just build the full path with realpath()and __DIR__will be the safest?

I also wonder if the doctrine requires autoload 1 to be configured for each namespace, and then click this autoloader?

then I wonder if the example is here ? basically he used something like ...

protected function _initDoctrine()
{
    // Create the doctrine autoloader and remove it from the spl autoload stack (it adds itself)
    require_once 'Doctrine/Common/ClassLoader.php';
    $doctrineAutoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass');
    spl_autoload_unregister($doctrineAutoloader);

    // Fetch the global Zend Autoloader
    $autoloader = Zend_Loader_Autoloader::getInstance();

    // Push the doctrine autoloader to load for the Doctrine\ namespace
    $autoloader->pushAutoloader($doctrineAutoloader, 'Doctrine\\');
}

, ClassLoader doctrine 2, , 1 1 . spl_autoload_unregister($doctrineAutoloader)?

+3
1

ok, ""

+3

Source: https://habr.com/ru/post/1757370/


All Articles