Skip to navigation Skip to content
Matthias Kerstner

Agile enthusiast, product-minded & user centric

  • Home
  • Blog
  • Contact
  • Home
  • Blog
  • Contact
Posted on 08/09/201420/08/2016 by Matthias Kerstner — Leave a comment

Integrating Doctrine 2 into your project using Composer

Doctrine Logo

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);

 

Categories: Doctrine, Frameworks, PHP
Tags: annotation driver, bootstrap, command line, composer, consolerunner, doctrine 2, entitymanager, integrate, meta data

Post navigation

Previous post: Doctrine 2 Exception EntityManager is closed
Next post: Multiple Google AdSense ad blocks on same page are causing error 400 Bad Response
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Imprint
  • Disclaimer and Privacy Policy

© 2023 Matthias Kerstner

© Matthias Kerstner 2023
Disclaimer and Privacy PolicyBuilt with Storefront.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
SAVE & ACCEPT