I use Zend Framework and Doctrine 2 in my project. Everything works fine except, that autoloader doesn't load Zend classes from Doctrine repositories.
Here is my bootstrap part for Zend autoloader:
/**
* Register namespace Default_
* #return Zend_Application_Module_Autoloader
*/
protected function _initAutoload() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Default_',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
Here is my bootstrap part for Doctrine initialization:
/**
* Initialize Doctrine
* #return Doctrine_Manager
*/
public function _initDoctrine() {
// include and register Doctrine's class loader
require_once('Doctrine/Common/ClassLoader.php');
$classLoader = new \Doctrine\Common\ClassLoader(
'Doctrine',
APPLICATION_PATH . '/../library/'
);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader(
'Repositories',
APPLICATION_PATH . '/../library/Model'
);
$classLoader->register();
// create the Doctrine configuration
$config = new \Doctrine\ORM\Configuration();
// setting the cache ( to ArrayCache. Take a look at
// the Doctrine manual for different options ! )
$cache = new \Doctrine\Common\Cache\ApcCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// choosing the driver for our database schema
// we'll use annotations
$driver = $config->newDefaultAnnotationDriver(
APPLICATION_PATH . '/../library/Model'
);
$config->setMetadataDriverImpl($driver);
// set the proxy dir and set some options
$config->setProxyDir(APPLICATION_PATH . '/../library/Model/Proxies');
$config->setAutoGenerateProxyClasses(true);
$config->setProxyNamespace('Model\Proxies');
// now create the entity manager and use the connection
// settings we defined in our application.ini
$connectionSettings = $this->getOption('doctrine');
$conn = array(
'driver' => $connectionSettings['conn']['driv'],
'user' => $connectionSettings['conn']['user'],
'password' => $connectionSettings['conn']['pass'],
'dbname' => $connectionSettings['conn']['dbname'],
'host' => $connectionSettings['conn']['host']
);
$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
// push the entity manager into our registry for later use
$registry = Zend_Registry::getInstance();
$registry->entitymanager = $entityManager;
return $entityManager;
}
How could i fix this?
Your _initAutoload is completely unnecessary.
Just add
autoloadernamespaces[] = Default
autoloadernamespaces[] = Doctrine
to your application.ini
I agree that _initAutoload() shouldn't be necessary, but i suspect you'll need this in application.ini:
autoloaderNamespaces[] = "Doctrine"
autoloaderNamespaces[] = "Model"
If you just starting you project I will advice to use zf2 (beta5 last beta folowing: http://framework.zend.com/zf2/blog/entry/Zend-Framework-2-0-0beta5-Released)
This is tutorial which I was using for my project zf2 + doctrine 2 http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/
Related
I have 1 zend (v1) application, and 2 module : default + admin
I want when call default module will be set router in configs/router/default.ini
and if in module admin do not any thing
I tried using plugin but it doesn't work
in my plugin
class Australian_Controller_Plugin_DefaultRouter extends Zend_Controller_Plugin_Abstract {
public function routeShutdown(Zend_Controller_Request_Abstract $request) {
$currModule = $request->getModuleName();
if ($currModule != 'default') {
return;
}
$fontController = Zend_Controller_Front::getInstance();
$router1 = new Zend_Controller_Router_Rewrite();
$fontController->getRouter()->removeDefaultRoutes();
$myRoutes = new Zend_Config_Ini(APPLICATION_PATH . '/configs/router/default.ini', 'production');
$router1->addConfig($myRoutes, 'routes');
$fontController->setRouter($router1);
}
}
and /default/Bootstrap.php
protected function _initRoutes() {
$fontController = Zend_Controller_Front::getInstance();
$fontController->registerPlugin(new Australian_Controller_Plugin_DefaultRouter());
}
thanks
Note that you are adding new router after Routing so Zend already decoded address using old routes. This way you can generate URLs using new routes, but they will not e recognized by Zend. You need to call $router->route($request); again to set module/controller/action using new set of routes.
Sadly this is not working when its called in routeShutdown and has to be added to preDispatch(). Sadly I'm quite new to Zend too and still not grasping why it is so.
Code i used:
$fontController = Zend_Controller_Front::getInstance();
$router = $fontController->getRouter();
$r = new Zend_Controller_Router_Route(
'/testnew',
array(
'module' => 'user',
'controller' => 'index',
'action' => 'myaccount',
));
$router->addRoute('testnew', $r);
$router->route($request);
I created ZendFramework 1.12 applications with modules.
The idea was to have versions of the API in modules, with RESTfull paths.
The default module was V1, and now when I am trying to create v1s1 module, all links still lead to V1.
Can anybody push me in the right direction how to solve this?
MORE DETAILS
This is the code I have to use modules.
application.ini
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.defaultModule = "v1"
resources.modules[] =
v1.boostrap.path = APPLICATION_PATH "/modules/v1/Bootstrap.php"
resources.frontController.plugins.putHandler = Zend_Controller_Plugin_PutHandler
routes.rest.type = Zend_Rest_Route
routes.rest.defaults.module = v1
Main Bootstrap.php:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAppAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'V1s1',
'basePath' => APPLICATION_PATH . '/modules/v1s1',
));
$autoloader->addResourceType('model', 'models', 'Model');
return $autoloader;
}
}
V1 Bootstrap.php:
class V1_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected $_moduleName = "v1";
protected function _initAutoload()
{
// Add autoloader empty namespace
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('v1_');
return $autoLoader;
}
protected function _initResourceLoader()
{
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/modules/v1/controllers/helpers');
}
public function _initRouts()
{
$front = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($front, array(), $arrayOfControllers));
$front->getRouter()->addRoute('rest', $restRoute);
}
}
Now I need to create module v2, but when I do that, I everything crashes. Link are no longer RESTfull, and every link still leads to the V1 module. V2 module Bootstrap.php is being accessed.
Please give me proper solution for my query. I have tried to solve it but not getting any proper solution. Please give me proper solution.
If I remove following line from application.ini file then it is working well for front end application
resources.modules[] =
After remove it, I am unable to get my created module(Admin) in modules folder with proper layout. I have only one module. In module bootstrap file I have defined following functions (project/application/modules/admin/Bootstrap.php)
<?php
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAppAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'admin',
'basePath' => APPLICATION_PATH . '/modules/admin/'
));
return $autoloader;
}
protected function _initPlugins()
{
$bootstrap = $this->getApplication();
if ($bootstrap instanceof Zend_Application) {
$bootstrap = $this;
}
$bootstrap->bootstrap('FrontController');
$front = $bootstrap->getResource('FrontController');
$plugin = new Admin_Plugin_Layout();
// $plugin->setBootstrap($this);
$front->registerPlugin($plugin);
}
protected function _initAuthPlugin()
{
$checkAuth = Zend_Controller_Front::getInstance();
$checkAuth->registerPlugin(new Admin_Plugin_CheckAuth(Zend_Auth::getInstance()));
}
protected function _initDoctype()
{
global $adminModuleCssPath;
global $adminModuleJsPath;
$this->bootstrap( 'view' );
$view = $this->getResource( 'view' );
$view->headTitle('Jyotish - Ek Gyan');
$view->headScript()->appendFile($adminModuleJsPath.'jquery-1.7.2.js');
$view->headScript()->appendFile($adminModuleJsPath.'jquery-ui.js');
$view->headScript()->appendFile($adminModuleJsPath.'tinybox.js');
$view->headScript()->appendFile($adminModuleJsPath.'common.js');
$view->headLink()->appendStylesheet($adminModuleCssPath.'jquery-ui.css');
$view->headLink()->appendStylesheet($adminModuleCssPath.'style.css');
$view->headLink()->appendStylesheet($adminModuleCssPath.'theme.css');
$view->headLink()->appendStylesheet($adminModuleCssPath.'tinybox.css');
$view->doctype( 'XHTML1_STRICT' );
//$view->navigation = $this->buildMenu();
}
protected function _initLayoutPlugin()
{
$layout = Zend_Controller_Front::getInstance();
$layout->registerPlugin(new Admin_Plugin_AdminLayout());
}
protected function _initRouter()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
':module/:controller/:action/*',
array('module' => 'admin')
);
$router->addRoute('default', $route);
$usersRoute = new Zend_Controller_Router_Route_Regex(
':module/:controller/:action/(?:/page/(\d+)/?)?',
array(
'module' => 'admin',
'controller' => 'users',
'action' => 'index',
'page' => 1,
),
array(
'page' => 1,
)
);
$router->addRoute('users-index', $usersRoute);
}
protected function _initActionHelpers()
{
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . "/modules/admin/views/helpers");
Zend_Controller_Action_HelperBroker::addPrefix('Admin_View_Helper');
}
}
In modules folder I have created following plugin Layout
class Admin_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
if ('admin' != $request->getModuleName()) {
// If not in this module, return early
return;
}
// Change layout
Zend_Layout::getMvcInstance()->setLayout('admin');
}
}
In frontend bootstrap file I have defined following functions(project/application/Bootstrap.php)
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAppAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'default',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
protected function _initLayoutHelper()
{
$this->bootstrap('frontController');
$layout = Zend_Controller_Action_HelperBroker::addHelper(
new Application_View_Helper_LayoutLoader());
}
}
I have created following helper file in (project/application/view/helper/LayoutLoader.php)
<?php
class Application_View_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract
{
public function preDispatch()
{
$bootstrap = $this->getActionController()
->getInvokeArg('bootstrap');
$config = $bootstrap->getOptions();
$module = $this->getRequest()->getModuleName();
if (isset($config[$module]['resources']['layout']['layout'])) {
$layoutScript =
$config[$module]['resources']['layout']['layout'];
$this->getActionController()
->getHelper('layout')
->setLayout($layoutScript);
}
}
}
From last two days I am trying to create separate layout for both but I am unable to getting proper solution. When I run admin module in browser, it is working well but when I run frontend application folder it show exception error with layout of admin.
Please provide me proper solution....
Thanks
The standard way to do a layout switching is with a front-controller plugin. You don't need the LayoutLoader helper with preDispatch hook.
A simple layout-switcher plugin can be implemented as follows.
Place your various layout files in application/layouts/scripts/, named the same as your module: default.phtml, admin.phtml, etc.
In the file application/plugins/Layout.php:
class Application_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
Zend_Layout::getMvcInstance()->setLayout($request->getModuleName());
}
}
Enable the plugin application/configs/application.ini using:
resources.frontController.plugins.layout = "Application_Plugin_Layout"
or by manually registering the plugin in Bootstrap.
Also, make sure your application.ini enables modules and identifies your layout location:
resources.modules[]=
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
You seem to be way over thinking this. I'm going to simplify this down to the basic and you can add back as you see fit.
The bootstrap system in ZF1 is a bad joke. Anything that is present in one of the bootstrap files will available to all modules at run and will be run with every request. So keep it simple and keep it light. I normally put all of the _init methods in the main bootstrap and leave the module bootstraps empty.
//simplify your bootstrap to minimum
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
//your app autoloader is the standard autoloader so is not needed here, it better just work.
/**
* initialize the session
*/
protected function _initsession()
{
Zend_Session::start();
}
/**
* initialize the registry and assign application.ini to config namespace
*/
protected function _initRegistry()
{
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
}
protected function _initView()
{
//Initialize view
$view = new Zend_View();
//add custom view helper path
$view->addHelperPath('/../library/My/View/Helper');
//set doctype for default layout
$view->doctype(Zend_Registry::get('config')->resources->view->doctype);
//set default title
$view->headTitle('Jyotish - Ek Gyan');
//set css includes
$view->headlink()->setStylesheet('/bootstrap/css/bootstrap.min.css');
//add javascript files
$view->headScript()->setFile('/bootstrap/js/jquery.min.js');
$view->headScript()->appendFile('/bootstrap/js/bootstrap.min.js');
//add it to the view renderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer');
$viewRenderer->setView($view);
//Return it, so that it can be stored by the bootstrap
return $view;
}
/**
* Not sure if this is really required as it seems as though your routes are pretty standard.
*/
protected function _initRouter()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
':module/:controller/:action/*',
array('module' => 'admin')
);
$router->addRoute('default', $route);
$usersRoute = new Zend_Controller_Router_Route_Regex(
':module/:controller/:action/(?:/page/(\d+)/?)?',
array(
'module' => 'admin',
'controller' => 'users',
'action' => 'index',
'page' => 1,
),
array(
'page' => 1,
)
);
$router->addRoute('users-index', $usersRoute);
}
protected function _initAuthPlugin()
{
$checkAuth = Zend_Controller_Front::getInstance();
$checkAuth->registerPlugin(new Admin_Plugin_CheckAuth(Zend_Auth::getInstance()));
}
}
The application.ini is a very important part of application configuration:
//truncated for example
[production]
;-------------------------------------------------------------------------------
;//PHP
;-------------------------------------------------------------------------------
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
;-------------------------------------------------------------------------------
;//Paths and Namespaces, paths for application resources and library
;-------------------------------------------------------------------------------
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
autoloaderNamespaces[] = "My_"
;-------------------------------------------------------------------------------
;//Front Controller, default settings for controllers and modules
;-------------------------------------------------------------------------------
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.frontController.params.prefixDefaultModule = ""
resources.modules = ""
resources.frontController.baseurl = http://example.com
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
;-------------------------------------------------------------------------------
;//plugins, default paths for resource plugins and action helpers, can also be
; //accomplished in the bootstrap
;-------------------------------------------------------------------------------
pluginPaths.My_Application_Resource = APPLICATION_PATH "/../library/My/Resource"
resources.frontController.actionhelperpaths.My_Controller_Action_Helper = APPLICATION_PATH "/../library/My/Controller/Action/Helper"
;-------------------------------------------------------------------------------
;//View Settings, view settings to be shared with the view object in the bootstrap
;// allows for easier editing.
;-------------------------------------------------------------------------------
resources.view[]=
resources.view.charset = "UTF-8"
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.language = "en"
resources.view.contentType = "text/html; charset=UTF-8"
;-------------------------------------------------------------------------------
;//Database Settings, default database adapter settings
;-------------------------------------------------------------------------------
resources.db.adapter = "pdo_Mysql"
resources.db.params.username = "user"
resources.db.params.password = "xxxx"
resources.db.params.dbname = "dbname"
resources.db.params.charset = "utf8"
resources.db.isDefaultTableAdapter = true
resources.db.params.profiler = true
;-------------------------------------------------------------------------------
;//Layouts, default path to layout files and default layout name
;-------------------------------------------------------------------------------
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = layout
Now the simplest way to accomplish layout switching is to switch the layout on a per controller basis, use the default layout except where you explicitly change it:
//some controller that needs the admin layout
//note: preDispatch() and routeShutdown() are essentially the same point in the loop
public function preDispatch() {
//switch to new layout
$this->_helper->layout->setLayout('admin');
//you can also manipulate the css and js similarly to the bootstrap
$this->view->headScript()->appendFile(
'/javascript/mediaelement/build/mediaelement-and-player.min.js');
$this->view->inlineScript()->setScript(
"$('audio').mediaelementplayer();");
}
I find that if I just need one or two different layouts this works nicely and is simple. Not really a DRY solution but can really help develop the parameters for a plugin if a DRY solution is desired.
I hope this provides some help, the big thing to remember is that the module bootstraps are basically just extentions of the main bootstrap and provide no seperation of functionality.
P.S. I would have provided a plugin demo but I really suck at plugins, Sorry.
I followed the steps in the following article in order to integrate Zend Framework 1.11 and Doctrine 2:
http://jeboy25.blogspot.com/2010/08/doctrine-2-and-zend-framework-110.html
And I have 3 questions about the article:
1-In the "SchemaToolClass" section i don't understand why the author includes schema_tool.php at the bottom of ZendProject/public/index.php file after :
$application->bootstrap()
->run();
2-when i execute the command "php doctrine orm:schema-tool:create" i have the following error message in the command line:
HP Stack trace:
PHP 1. {main}() /Library/WebServer/Documents/carlending/application/tools/doctrine:0
PHP 2. include() /Library/WebServer/Documents/carlending/application/tools/doctrine:7
PHP 3. require() /Library/WebServer/Documents/carlending/application/tools/doctrine.php:41
PHP 4. Doctrine\Common\ClassLoader->loadClass($className = uninitialized)
the error occurs in the cli-config.php file at the line '$config = new \Doctrine\ORM\Configuration();'
3-Can you explain why the author puts the doctrine generated proxies and models inside the domain folder. Isn't it better that they reside in the models folder like any other model class.
Sometimes i also see some programmers using a 'generated' folder inside models.
If you managed to make a working integration of Zend 1.x and Doctrine i would be very happy if you could also send me a working project that would very helpful.
Thanks for your help.
After reading "Obtaining the EntityManager" section in http://www.doctrine-project.org/docs/orm/2.1/en/tutorials/getting-started-xml-edition.html
I think you need the following three lines to bootstrap:
use Doctrine\ORM\Tools\Setup;
require_once 'Doctrine/ORM/Tools/Setup.php';
Setup::registerAutoloadPEAR();
I've got it to work a few weeks ago, here's my code. Doctrine 2 is really nice :)
In my bootstrap
/**
* Initialize auto loader of Doctrine
*
* #return Doctrine_Manager
*/
protected function _initDoctrine() {
$this->bootstrap('autoload');
require_once('Doctrine/Common/ClassLoader.php');
// 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');
//$doctrineAutoloader->register();
spl_autoload_unregister($doctrineAutoloader);
$autoloader = Zend_Loader_Autoloader::getInstance();
// Push the doctrine autoloader to load for the Doctrine\ namespace
$autoloader->pushAutoloader($doctrineAutoloader, 'Doctrine');
$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(__DIR__ . '/models/'), 'loadClass');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', realpath(__DIR__ . '/../library/Doctrine/'), 'loadClass');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Symfony');
$doctrineConfig = $this->getOption('doctrine');
$config = new \Doctrine\ORM\Configuration();
$cache = new \Doctrine\Common\Cache\ArrayCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$driverImpl = new Doctrine\ORM\Mapping\Driver\YamlDriver(APPLICATION_PATH . '/../configs/mappings/yaml');
//$driverImpl = $config->newDefaultAnnotationDriver($doctrineConfig['path']['entities']);
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir(APPLICATION_PATH . '/../proxies');
$config->setProxyNamespace('App\Proxies');
$connectionOptions = array(
'driver' => $doctrineConfig['conn']['driv'],
'user' => $doctrineConfig['conn']['user'],
'password' => $doctrineConfig['conn']['pass'],
'dbname' => $doctrineConfig['conn']['dbname'],
'host' => $doctrineConfig['conn']['host']
);
$registry = Zend_Registry::getInstance();
$registry->entitymanager = $em;
return $em;
}
Schema etc
I use yaml as you seen above, I haven't looked through the tutorial but I've used the command line tool that works like a charm, my doctrine.php (located in APPLICATION/bin):
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/..'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
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');
require_once __DIR__ . '/../Bootstrap.php';
$em = $application->getBootstrap()->getResource('doctrine');
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em, APPLICATION_PATH . "/configs/mappings")
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
You first have to generate your entities:
Generate all the models without deleting, creates also annotations -
./doctrine orm:generate-entities ~/Public/my_app/application/models/ --regenerate-entities 0 --generate-annotations 1
then generate your schema
./doctrine orm:schema-tool:create --dump-sql
or
./doctrine orm:schema-tool:update --dump-sql
the proxies are not really a part of your models, it's just used by Doctrine internally so I've put it as a separate entity from the models folder but I guess it doesn't really matter:
./doctrine orm:generate-proxies ~/Public/my_app/proxies/
Remember to add write permissions to the proxies for apache group.
Hmm... guess I didn't quite explain Jeboy's solution but perhaps my code helps you get started, it took me a while but once it's up and running it works like a charm :)
PS Don't forget the "namespace Entities;" in each of your models (it should be generated automatically)
i seem to managed to integrate Doctrine 2's autoloaders to Zend's, tho i am not sure if i am doing it correctly ...
directory 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;
1 question i have is can i use relative paths in newDefaultAnnotationDriver() and setProxyDir() isit safer to say that i just build the full path using realpath() and __DIR__ will be the safest?
i also wonder if doctrine requires me to setup autoloading 1 for each namespace, then push that autoloader?
then i wonder if the example here is workable? 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\\');
}
i wonder why he dont need to pass parameters into ClassLoader from the doctrine 2 sandbox, namespaces are passed into the autoloader, 1 by 1 and the autoloader registered. and why is spl_autoload_unregister($doctrineAutoloader) required?
ok i just say yes and close this