Hard way
You need to write two files: bootstrap.php and cli-config.php
Why should I write two files, yes?
Doctrine documents will instruct you to do this.
Where does it say that?
Here:
In cli-config find the code
Here GetEntityManager() not part of any library or code, it is your own custom method that Doctrine offers you to write yourself, where this method is what returns the instance of Doctrine\ORM\EntityManager . I think the message of the Doctrine could be clearer in this part.
I still can not do this, Documents are too hard, help!
If your configuration has too many layers, that is, you are trying to develop Doctrine with ZF2 or Symphony, find out where Doctrine ends where the framework starts, etc. Try to configure Doctrine yourself in a separate container for testing, following the Doctrine docs, without using any framework, everything should become very very clear.
-
Here is what I did for my Doctrine config. I decided to create 3 files for more flexibility and put them in my ./config folder in my Zend folder structure.
Firstly, I have cli-config.php
use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Console\ConsoleRunner; require 'config/bootstrap.php'; $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode); $entityManager = EntityManager::create($dbParams, $config); return ConsoleRunner::createHelperSet($entityManager);
Then my bootstrap.php
date_default_timezone_set("America/Detroit"); // Set the default timezone ini_set('display_startup_errors', 1); ini_set('display_errors', 1); error_reporting(- 1); include 'vendor/autoload.php'; include 'doctrine-config.php';
And finally, my doctrine-config.php . It is separate for an easy way to call only this file from other parts of your code. In my case, this is called for the CLI and inside the ZF2 singleton when I need a connection to Doctrine.
// Paths to Entities that we want Doctrine to see $paths = array( "module/Module/src/Entity", "module/MyApplication/src/Entity" ); // Tells Doctrine what mode we want $isDevMode = true; // Doctrine connection configuration $dbParams = array( 'driver' => 'pdo_mysql', 'user' => 'user', 'password' => 'pass', 'dbname' => 'db_name' );