Doctrine represents one of the best-known and performant database libraries today. It is comprised of a set of stable and highly performant libaries for database storage (Doctrine DBAL) and object mapping (Doctrine ORM). This post describes the steps needed to integrate Doctrine 2 into your project using composer.
Setup using Composer
First, make sure that you have installed composer and set it up correctly. The following composer.json configuration shows the setup required to install Doctrine 2:
{ "require": { "php": ">=5.4.0", "doctrine/orm": "2.*", "doctrine/instantiator": "1.*", "symfony/console": "2.*", "symfony/yaml": "2.*" }, "autoload": { "psr-0": { "Doctrine\\ORM\\": "lib/" } }, "bin": ["bin/doctrine", "bin/doctrine.php"], "archive": { "exclude": ["!vendor", "tests", "*phpunit.xml", ".travis.yml", "build.xml", "build.properties", "composer.phar", "lib/vendor", "*.swp", "*coveralls.yml"] } }
Install Doctrine 2
To install and activate Doctrine 2 run the composer update command. Now create a bootstrap.php to initialize Doctrine for requests to your app:
use Doctrine\Common\Cache; use Doctrine\ORM\EntityManager; $loaderPath = __DIR__ . '/vendor/autoload.php'; if (!is_readable($loaderPath)) { throw new LogicException('Run php composer.phar install at first'); } $loader = require $loaderPath; $loader->add('Entities', __DIR__); $loader->add('Proxies', __DIR__); $debug = true; $config = new \Doctrine\ORM\Configuration(); // Set up Metadata Drivers $driverImpl = $config->newDefaultAnnotationDriver(array(__DIR__ . "/Entities")); $config->setMetadataDriverImpl($driverImpl); // Optionally, set up caches, depending on $debug variable. //$cache = $debug ? new Cache\ArrayCache : new Cache\ApcCache; //$config->setMetadataCacheImpl($cache); //$config->setQueryCacheImpl($cache); // Proxy configuration $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('Proxies'); // Database connection information $connectionOptions = array( 'driver' => 'pdo_mysql', 'user' => '', 'password' => '', 'dbname' => '' ); // Optionally, enable second-level cache //$cacheConfig = new \Doctrine\ORM\Cache\CacheConfiguration(); //$cacheDriver = $debug ? new Cache\ArrayCache : new Cache\ApcCache; //$cacheLogger = new \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger(); //$factory = new \Doctrine\ORM\Cache\DefaultCacheFactory($cacheConfig->getRegionsConfiguration(), $cacheDriver); //if ($debug) { // $cacheConfig->setCacheLogger($cacheLogger); //} //$cacheConfig->setCacheFactory($factory); //$config->setSecondLevelCacheEnabled(true); //$config->setSecondLevelCacheConfiguration($cacheConfig); // finally create EntityManager instance that you will use in your application $entityManager = EntityManager::create($connectionOptions, $config);
Based on this setup you need to put your Entity model classes in the folder names Entities and proxy classes in Proxies folder. Also, this setup uses the default meta data annotation driver, i.e. add your meta data annotation to your Entity classes directly.
Setup Command Line Support
In order to enable Doctrine 2 from the command line you need to setup ConsoleRunner:
use Doctrine\ORM\Tools\Console\ConsoleRunner; // use bootstrap from above to setup Doctrine require_once 'bootstrap.php'; return ConsoleRunner::createHelperSet($entityManager);