Using Doctrine 2 with Zend Framework 1.10.x

How can I get started with Doctrine 2 + ZF? any tutorials or resources?

on a side note, I hear that ZF2 will use Doctrine as its models, right?

+3
source share
2 answers

What is nice to work with ZF and Doctrine 2 is that there is very little to integrate them. Essentially, you just need access to the Doctrine 2 instance EntityManagerthat was configured at bootstrap boot time, and also make sure that the Doctrine namespaces are loaded into yours index.php(you need to use the Doctrine ClassLoader for this, Zend_Loaderit doesn't support namespaces yet).

EntityManager , , Resource Plugin ( application.ini). Doctrine init() .

, EM , . . .

Doctrine 2 ZF, alpha, .

+3

bootstrap.php. :

    public function _initDoctrine() {
                if(PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3) {
                    require_once('bootstrapDoctrine.inc.php');

                    //things below this line are for convenience        
                    require_once(dirname(__FILE__).'/../library/doctrinehelpers/requireEntitiesOnce.php');
                    Zend_Registry::set('doctrineEm', $em);
                    return $em;
                }
        }

boostrapDoctrine.inc.php :

use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Configuration,
    Doctrine\ORM\EntityManager,
    Doctrine\Common\Cache\ArrayCache,
    Doctrine\DBAL\Logging\EchoSQLLogger;

    require_once(realpath(APPLICATION_PATH . '/../library').'/doctrine2/lib/Doctrine/Common/ClassLoader.php');

    $doctrineClassLoader = new ClassLoader('Doctrine', realpath(APPLICATION_PATH . '/../library').'/doctrine2/lib');
    $doctrineClassLoader->register();


    //no way to have your proxies generated in different directory per ZF module it seems so we use a global one
    $proxiesClassLoader = new ClassLoader('Proxies', realpath(APPLICATION_PATH . '/models/doctrineproxies'));
    $proxiesClassLoader->register();

    /*
     * @TODO make this step iterate over available modules
     */
        $driverImpl = $config->newDefaultAnnotationDriver(array(APPLICATION_PATH . '/modules/mymodule1/models/doctrineentities',APPLICATION_PATH . '/modules/mymodule2/models/doctrineentities'));
    $config->setMetadataDriverImpl($driverImpl);
    $config->setMetadataCacheImpl($cache);
    $config->setQueryCacheImpl($cache);

    // Proxy configuration
    $config->setProxyDir(realpath(APPLICATION_PATH . '/models/doctrineproxies'));
    $config->setProxyNamespace('Proxies');


    /**
     * this SQL logger is golden
         * @TODO implement a switch for verbose debugging
     */ 

     //   $logger = new Doctrine\DBAL\Logging\DebugStack();
     //   $config->setSQLLogger($logger);
     //   register_shutdown_function(function($logger) {
     //       echo '<pre>';
     //       print_r($logger->queries);
     //   }, $logger);


    $config->setAutoGenerateProxyClasses( true ); //disable in production environment

    $doctrineConfig = $this->getOption('resources'); //from ini
    $dbparams = $doctrineConfig['db']['params'];
    $connectionOptions = array(
         'driver'    => $doctrineConfig['db']['adapter'],
         'user'      => $dbparams['username'],
         'password'  => $dbparams['password'],
         'dbname'    => $dbparams['dbname'],
         'host'      => $dbparams['host']
    );
    $em = EntityManager::create($connectionOptions, $config); //stored in zend registry later

doctrine , /doctrine 2/lib/cli-config.php, zend framework. , cli . ;)

/*
* @TODO make the cli-tool more flexible by better path detection
*/
// Define path to application directory
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/../../include'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);


$    application->getBootstrap()->bootstrap('doctrine');
$em = $application->getBootstrap()->getResource('doctrine');

/*
$configuration = new \Doctrine\Common\Cli\Configuration();
$configuration->setAttribute('em', $em);
*/

$helperSet = new \Symfony\Components\Console\Helper\HelperSet(array(
    'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
    'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));

, ZF 2, , , allready.

, .

+2

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


All Articles