I've been using Doctrine in my project without explicitly namespacing any of my classes. This led to some problems with trying to organise my code into separate sub directories (or at least it seemed to). As such I've tried to implement namespaces in my code but I'm struggling and having tried the numerous solutions on here to no avail, I need to ask.
I've got the standard project structure:
application/
--models/
--services/
--controllers/
..etc
In my Bootstrap I've got the following (without namespaces in my code which works fine):
/**
* Initialize Doctrine
* #return Doctrine_Manager
*/
public function _initDoctrine() {
// include and register Doctrine's class loader
require_once('doctrine/Doctrine/Common/ClassLoader.php');
$autoloader = \Zend_Loader_Autoloader::getInstance();
require_once('doctrine/Doctrine/Common/ClassLoader.php');
$commonLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', 'doctrine');
$autoloader->pushAutoloader(array($commonLoader, 'loadClass'), 'Doctrine\Common');
$dbalLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', 'doctrine');
$autoloader->pushAutoloader(array($dbalLoader, 'loadClass'), 'Doctrine\DBAL');
$ormLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', 'doctrine');
$autoloader->pushAutoloader(array($ormLoader, 'loadClass'), 'Doctrine\ORM');
$modelLoader = new \Doctrine\Common\ClassLoader(NULL, APPLICATION_PATH . "/models");
$autoloader->pushAutoloader(array($modelLoader, 'loadClass'), '');
// 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\ArrayCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// choosing the driver for our database schema
// we'll use annotations
$driver = $config->newDefaultAnnotationDriver(
APPLICATION_PATH . '/models'
);
$config->setMetadataDriverImpl($driver);
// set the proxy dir and set some options
$config->setProxyDir(APPLICATION_PATH . '/models/Proxies');
$config->setAutoGenerateProxyClasses(true);
//$config->setAutoGenerateProxyClasses(false);
$config->setProxyNamespace('App\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;
}
When I add
namespace models;
to each of my model classes and update the Bootstrap to be as follows I get an Exception "Class Application does not exist" (Application is one of my models):
$modelLoader = new \Doctrine\Common\ClassLoader('models', APPLICATION_PATH . "/models");
$autoloader->pushAutoloader(array($modelLoader, 'loadClass'), 'models');
Just for completeness, I reference that model in my controller as follows:
public function indexAction()
{
$this->_helper->layout()->title = "Your applications";
$this->_helper->layout()->description = "Create, edit and view all applications you have registered with us.";
$this->view->applicationList = $this->entityManager->getRepository("Application")->findAll();
}
What am I missing? I'm sure it's obvious but really am pulling out my hair now.
After much futher searching, I stumbled across this video (login required).
Basically, the way I solved (as per the webinar) was to namespace my entities and save them under the /library directory. Then whenever I need to use them from the actual app, I access via their namespace.
Related
We are using Doctrine 2 in our app, but due to our infrastructure, we do not have a static configuration for database connections. Instead, we have a collection of singletons in a service provider for each database we need to connect to, and we select a random database host for then when we connect.
Unfortunately, we are seeing some performance degradation in Doctrine's getRepository() function. I believe the issue is that Doctrine needs to generate its proxy classes at runtime (even in production) because we cannot figure out how to configure the CLI tools in order to create them at build time.
We are using the Laravel framework for the application.
Here's an example of our Laravel service provider which makes the repositories available for dependency injection.
<?php
use App\Database\Doctrine\Manager as DoctrineManager;
use Proprietary\ConnectionFactory;
use App\Database\Entities;
use App\Database\Repositories;
use App\Database\Constants\EntityConstants;
class DoctrineServiceProvider extends ServiceProvider
{
// Create a singleton for the Doctrine Manager. This class will handle entity manager generation.
$this->app->singleton(DoctrineManager::class, function ($app)
{
return new DoctrineManager(
$app->make(ConnectionFactory::class),
[
EntityConstants::ENTITY_CLASS_DATABASE1 => [app_path('Database/Entities/Database1')],
EntityConstants::ENTITY_CLASS_DATABASE2 => [app_path('Database/Entities/Database2')],
],
config('app.debug'),
$this->app->make(LoggerInterface::class)
);
});
// Register the first repository
$this->app->singleton(Repositories\Database1\RepositoryA1::class, function ($app)
{
return $app[DoctrineManager::class]
->getEntityManager(EntityConstants::ENTITY_CLASS_DATABASE1)
->getRepository(Entities\Database1\RepositoryA1::class);
});
// Register the second repository
$this->app->singleton(Repositories\Database1\RepositoryA2::class, function ($app)
{
return $app[DoctrineManager::class]
->getEntityManager(EntityConstants::ENTITY_CLASS_DATABASE1)
->getRepository(Entities\Database1\RepositoryA2::class);
});
// Register a repository for the second database
$this->app->singleton(Repositories\Database2\RepositoryB1::class, function ($app)
{
return $app[DoctrineManager::class]
->getEntityManager(EntityConstants::ENTITY_CLASS_DATABASE2)
->getRepository(Entities\Database2\RepositoryB1::class);
});
}
Here's the class that generates EntityManagers for Doctrine:
<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Proprietary\ConnectionFactory;
class Manager
{
private $c_factory;
private $paths;
private $connections = [];
private $entity_managers = [];
public function __construct(
ConnectionFactory $cf,
array $paths
)
{
$this->c_factory = $cf;
$this->paths = $paths;
}
public function getConnection($name, $partition = false, $region = false)
{
// Get a list of servers for this database and format them for use with Doctrine
$servers = self::formatServers($name, $this->c_factory->getServers($name, true, $partition, $region));
// Generate a connection for the entity manager using the servers we have.
$connection = DriverManager::getConnection(
array_merge([
'wrapperClass' => MasterSlaveConnection::class,
'driver' => 'pdo_mysql',
], $servers)
);
return $connection;
}
public function getEntityManager($name, $partition = false, $region = false)
{
// Should these things be cached somehow at build time?
$config = Setup::createAnnotationMetadataConfiguration($this->paths[$name], false);
$config->setAutoGenerateProxyClasses(true);
// Set up the connection
$connection = $this->getConnection($name, $partition, $region);
$entity_manager = EntityManager::create($connection, $config);
return $entity_manager;
}
// Converts servers from a format provided by our proprietary code to a format Doctrine can use.
private static function formatServers($db_name, array $servers)
{
$doctrine_servers = [
'slaves' => [],
];
foreach ($servers as $server)
{
// Format for Doctrine
$server = [
'user' => $server['username'],
'password' => $server['password'],
'host' => $server['hostname'],
'dbname' => $db_name,
'charset' => 'utf8',
];
// Masters can also be used as slaves.
$doctrine_servers['slaves'][] = $server;
// Servers are ordered by which is closest, and Doctrine only allows a
// single master, so if we already set one, don't overwrite it.
if ($server['is_master'] && !isset($doctrine_servers['master']))
{
$doctrine_servers['master'] = $server;
}
}
return $doctrine_servers;
}
}
Our service classes use dependency injection to get the repository singletons defined in the service provider. When we use the singletons for the first time, Doctrine will use the entity class defined in the service provider and get the connection associated with the repository.
Is there any way we can enable the CLI tools with this configuration? Are there any other ways that we can optimize this for use in production?
Thanks.
I was able to solve the problem thanks to a suggestion from the Doctrine IRC channel. Since the CLI tools can only handle a single database, I created a doctrine-cli directory containing a base-config.php file and a subdirectory for each of the databases we use.
Here's an example file structure:
doctrine-cli/
|- database1/
| |- cli-config.php
|- database2/
| |- cli-config.php
|- base-config.php
The base-config.php file looks like this:
<?php
use Symfony\Component\Console\Helper\HelperSet;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use App\Database\Doctrine\Manager as DoctrineManager;
use Proprietary\ConnectionFactory;
require __DIR__ . '/../bootstrap/autoload.php';
class DoctrineCLIBaseConfig
{
private $helper_set;
public function __construct($entity_constant, $entity_namespace)
{
$app = require_once __DIR__ . '/../bootstrap/app.php';
// Proprietary factory for getting our databse details
$connection_factory = new ConnectionFactory(...);
// Our class that parses the results from above and handles our Doctrine connection.
$manager = new DoctrineManager(
$connection_factory,
[$entity_constant => [app_path('Database/Entities/' . $entity_namespace)]],
false,
null,
null
);
$em = $manager->getEntityManager($entity_constant);
$this->helper_set = new HelperSet([
'db' => new ConnectionHelper($em->getConnection()),
'em' => new EntityManagerHelper($em),
]);
}
public function getHelperSet()
{
return $this->helper_set;
}
}
Here's an example cli-config.php from the database directory:
<?php
use App\Database\Constants\EntityConstants;
require __DIR__ . "/../base-config.php";
$config = new DoctrineCLIBaseConfig(
EntityConstants::ENTITY_CLASS_DATABASE1,
"database1"
);
return $config->getHelperSet();
Now, I'm able to cycle through each of the directories and run commands like so:
php ../../vendor/bin/doctrine orm:generate-proxies
For our build process, I wrote a simple shell script that cycles through the directories and runs the orm:generate-proxies command.
I'm writing my own PHP framework built on top of Symfony components as a learning exercise. I followed the tutorial found at http://symfony.com/doc/current/create_framework/index.html to create my framework.
I'd now like to wire up my routes against my controllers using annotations. I currently have the following code to setup the routing:
// Create the route collection
$routes = new RouteCollection();
$routes->add('home', new Route('/{slug}', [
'slug' => '',
'_controller' => 'Controllers\HomeController::index',
]));
// Create a context using the current request
$context = new RequestContext();
$context->fromRequest($request);
// Create the url matcher
$matcher = new UrlMatcher($routes, $context);
// Try to get a matching route for the request
$request->attributes->add($matcher->match($request->getPathInfo()));
I have come across the following class to load the annotations but I'm not sure how to use it:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php
I'd appreciate it if someone could help.
Thanks
I've finally managed to get this working. First I changed where I included the autoload.php file to the following:
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__ . '/../vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
Then I changed the routes collection bit (in the question) to:
$reader = new AnnotationReader();
$locator = new FileLocator();
$annotationLoader = new AnnotatedRouteControllerLoader($reader);
$loader = new AnnotationDirectoryLoader($locator, $annotationLoader);
$routes = $loader->load(__DIR__ . '/../Controllers'); // Path to the app's controllers
Here's the code for the AnnotatedRouteControllerLoader:
class AnnotatedRouteControllerLoader extends AnnotationClassLoader {
protected function configureRoute(Route $route, ReflectionClass $class, ReflectionMethod $method, $annot) {
$route->setDefault('_controller', $class->getName() . '::' . $method->getName());
}
}
This has been taken from https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/Routing/AnnotatedRouteControllerLoader.php. You may wish to modify it to support additional annotations.
I hope this helps.
I try to use Gedmo Loggable extensions for Doctrine2 but I got an error and I didn't understand why.
I got the error
Fatal error: Uncaught Doctrine\ORM\Mapping\MappingException: Class
"Gedmo\Loggable\Entity\LogEntry" sub class of
"Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry" is not a
valid entity or mapped super class. in
C:\Projects\easyprod\librairies\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php
on line 340
when I run
$article = new \classeDoctrine\Article;
$article->setTitle('my title');
$oGestionnaireEntites->persist($article);
$oGestionnaireEntites->flush();
$article = $oGestionnaireEntites->find('\classeDoctrine\Article', 1);
$article->setTitle('my new title');
$oGestionnaireEntites->persist($article);
$oGestionnaireEntites->flush();
//$repo = $em->getRepository('Gedmo\Loggable\Entity\LogEntry');
$repo = $oGestionnaireEntites->getRepository('Gedmo\Loggable\Entity\LogEntry'); // we use default log entry class
$article = $oGestionnaireEntites->find('classeDoctrine\Article', 1);
$logs = $repo->getLogEntries($article);
// lets revert to first version
$repo->revert($article, 1);
// notice article is not persisted yet, you need to persist and flush it
echo $article->getTitle(); // prints "my title"
$oGestionnaireEntites->persist($article);
$oGestionnaireEntites->flush();
configuration file-
use Doctrine\ORM\EntityManager,
Doctrine\ORM\Configuration,
Doctrine\ORM\Tools\Setup,
Doctrine\ORM\Proxy\Autoloader;
use Doctrine\Common\Annotations\AnnotationRegistry;
//charge l'autoload voir composer.json
require_once ROOT . "/librairies/autoload.php";
// First of all autoloading of vendors
$loader = require ROOT . "/librairies/autoload.php";
// gedmo extensions
$loader->add('Gedmo', ROOT . "/librairies/gedmo/doctrine-extensions/lib");
$tCheminEntites = array(
ROOT . '/class/doctrine',
ROOT . '/class/doctrine/entite/grilleResultat',
ROOT . '/class/doctrine/entite/analyse',
ROOT . '/class/doctrine/entite/matrice',
ROOT . '/class/doctrine/entite/test',
);
foreach ($tCheminEntites as $sCheminEntite) {
// autoloader for Entity namespace
$loader->add('classeDoctrine', $sCheminEntite);
}
//Paramètre de la base de données
$tBddParametres = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'easyprod',
'charset' => 'UTF8'
);
Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
ROOT . '/librairies/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
);
$bModeDeveloppement = true;
// globally used cache driver, in production use APC or memcached
$cache = new Doctrine\Common\Cache\ArrayCache;
// standard annotation reader
$annotationReader = new Doctrine\Common\Annotations\SimpleAnnotationReader();
$annotationReader->addNamespace('Doctrine\ORM\Mapping');
$cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader(
$annotationReader, // use reader
$cache // and a cache driver
);
// create a driver chain for metadata reading
$driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain();
// load superclass metadata mapping only, into driver chain
// also registers Gedmo annotations.NOTE: you can personalize it
Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
$driverChain, // our metadata driver chain, to hook into
$cachedAnnotationReader // our cached annotation reader
);
// now we want to register our application entities,
// for that we need another metadata driver used for Entity namespace
$annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(
$cachedAnnotationReader, // our cached annotation reader
$tCheminEntites// paths to look in
);
// NOTE: driver for application Entity can be different, Yaml, Xml or whatever
// register annotation driver for our application Entity namespace
$driverChain->addDriver($annotationDriver, 'classeDoctrine');
//$oDefaultAnnotationDriver = $config->newDefaultAnnotationDriver($tCheminEntites);
// NOTE: driver for application Entity can be different, Yaml, Xml or whatever
// register annotation driver for our application Entity fully qualified namespace
//$driverChain->addDriver($oDefaultAnnotationDriver, 'classeDoctrine');
// general ORM configuration
$config = new Doctrine\ORM\Configuration;
$config->setProxyDir(sys_get_temp_dir());
$config->setProxyNamespace('Proxy');
$config->setAutoGenerateProxyClasses(false); // this can be based on production config.
// register metadata driver
$config->setMetadataDriverImpl($driverChain);
// use our already initialized cache driver
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// create event manager and hook preferred extension listeners
$evm = new Doctrine\Common\EventManager();
// gedmo extension listeners, remove which are not used
// loggable, not used in example
$loggableListener = new Gedmo\Loggable\LoggableListener;
$loggableListener->setAnnotationReader($cachedAnnotationReader);
$evm->addEventSubscriber($loggableListener);
//Paramètre de la base de données
$tBddParametres = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'easyprod',
'charset' => 'UTF8'
);
$oGestionnaireEntites = EntityManager::create($tBddParametres, $config, $evm);
$oGestionnaireEntites->getConfiguration()->addCustomHydrationMode('HydratorValeurSansNomColonne', 'boiteAOutils\HydratorValeurSansNomColonne');
Trying to add Doctrine DBAL into my own project to use it to access my db etc. I don't have composer and i never used it. This is what i am trying to do according to the docu:
use Doctrine\Common\ClassLoader;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
require_once "doctrine/Common/ClassLoader.php";
$classLoader = new ClassLoader('Doctrine', 'doctrine');
$classLoader->register();
$config = new Configuration();
$connectionParams = array(
'dbname' => 'mydb',
'user' => 'root',
'password' => "",
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
}
This is taken from here:
-http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html
and:
- http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/introduction.html
I have the Common and DBAL folder added into my project
My folder structure looks like this:
root
doctrine
DBAL
Common
php stuff
index.php (where connection.php) is executed
So what happens is that i either get "Cannot find class XY" or something similar, based upon what i change on the code. I never am able to execute it as it should following the tutorial.
What am i doing wrong here?
I just want to have the connection object, where i can start doing my stuff like useing the query builder etc...
I am completely lost here...
UPDATE: Installed composer as requested and have this Code now:
use Doctrine\DBAL\Configuration;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
$config = new Configuration();
$connectionParams = array(
'url' => 'mysql://root:secret#localhost/mydb',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
Which is the 2nd code example in my 1st link. Tells me " Class 'Doctrine\DBAL\Configuration' not found ". Funny thing is, that IntelliJ can perfectly autocomplete the path (suggests me Configuration when finishing DBAL in the path) but PHP doesn't find it. If i remove the new Configuration PHP just tells me, that it doesn't find the DriverManager...
I installed it correctly via composer though, at least composer tells me it is installed correctly now (Where does composer save the libs?)
You now need to require composers autoload file.
require __DIR__.'/vendor/autoload.php';
use Doctrine\DBAL\Configuration;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
$config = new Configuration();
$connectionParams = array(
'url' => 'mysql://root:secret#localhost/mydb',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
Please note, depending on your directory structure, the autoload file might be somewhere else, but usually this should work.
Pay attention to the use of namespaces: if the Doctrine namespace for its loader is Doctrine\Common\ClassLoader, you have to put the files inside the Doctrine\Common folder ("Doctrine" with a capital "D").
See the code snippet shown inside the Introduction chapter of the Doctrine DBAL documentations.
how can i get started with Doctrine 2 + ZF? any tutorials or resources?
on a side note, i hear ZF2 will use Doctrine as its models, true?
What's nice about working with ZF and Doctrine 2 is that there's very little that needs to be done to integrate them. Essentially, you just need access to an instance of Doctrine 2's EntityManager that's set up during the applications bootstrap, as well as make sure the Doctrine namespaces are getting loaded in your index.php (You'll need to use Doctrine's ClassLoader for this, Zend_Loader does not support namespaces yet).
You can instantiate your EntityManager manually in the bootstrap, or even easier, through a Resource Plugin (which makes it easy to store the database config in application.ini). You can pretty much follow the documentation on configuring and obtaining an Entity Manager in Doctrine's manual, and just have the init() method in your bootstrap resource return the instance.
You're probably going to want to depend highly on dependency injection for passing the EM to the various objects that need it. For an easy way to pass bootstrap resources into your action controllers, see this article on creating a simple resource injector.
I've been using Doctrine 2 with ZF since it's been in alpha, and have found it quite pleasant to work with.
I can give you some examples from the bootstrap.php I use:
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;
}
}
And in the boostrapDoctrine.inc.php I have this:
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
To allow the doctrine commandline tool to function I had to create a library/doctrine2/lib/cli-config.php which is also a stripped down zend framework bootstrap. This config has the disadvantage that I have to call the doctrine cli from within this directory. Works for me ;)
/*
* #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)
));
Now we are all hoping for a better doctrine integration but this will only happen in ZF 2 going a big step towards namespaces like doctrine did allready.
Hope I could help.