簡體   English   中英

將Doctrine 2與Zend Framework 1.10.x一起使用

[英]Using Doctrine 2 with Zend Framework 1.10.x

我如何開始使用Doctrine 2 + ZF? 任何教程或資源?

在旁注中,我聽說ZF2將使用Doctrine作為其模型,是真的嗎?

使用ZF和Doctrine 2的好處是,要集成它們幾乎沒有什么需要做的。 基本上,您只需要訪問在應用程序引導期間設置的Doctrine 2的EntityManager ,並確保在index.php加載Doctrine命名空間(您需要使用Doctrine的ClassLoader, Zend_Loader尚不支持名稱空間)。

您可以在引導程序中手動實例化您的EntityManager ,甚至更容易通過Resource Plugin實例化(這使得在application.ini存儲數據庫配置變得容易)。 您幾乎可以按照Doctrine手冊中有關配置和獲取實體管理器的文檔進行操作,只需讓bootstrap資源中的init()方法返回實例即可。

您可能希望高度依賴依賴注入來將EM傳遞給需要它的各種對象。 有關將引導程序資源傳遞到操作控制器的簡單方法,請參閱有關創建簡單資源注入器的文章

我一直在使用帶有ZF的Doctrine 2,因為它已經處於alpha狀態,並且發現使用它非常愉快。

我可以給你一些我使用的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命令行工具能夠運行,我必須創建一個庫/ doctrine2 / lib / cli-config.php,它也是一個精簡的zend框架引導程序。 這個配置的缺點是我必須從這個目錄中調用doctrine 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中,朝着像doctrine這樣的命名空間邁出了一大步。

希望我能提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM