Code First Approaching PHP with Doctrine 2.2.1 and Composer

A quick way to develop PHP / Database applications. In a few steps you will pass from a quick model YAML definition to a full database scheme and the PHP methods to abstract and access it, all via Doctrine ORM.

Download and install

You can download the project at And there is no install :D just some config … keep reading …


You can connect to multiple databases, and they are defined at step 6 in bootstrap.php. Also, you can add dependencies to composer. No more config files.

Now, execute getcomposer.bat to download and install composer. After composer finishes the download, execute php composer.phar install to downoad and install all the dependencies. The dependencies are defined in composer.json, and they are:

“require”: {
“doctrine/common”: “2.2.*”,
“doctrine/dbal”: “2.2.*”,
“doctrine/orm”: “2.2.*”,

Bootstrapping and scripts …

There is a number of script files included (i developed this thing with aptana/Win64):

  • Execute getgomposer.bat to obtain composer
  • Execute createproject.bat to create BBDD from YAML definitions
  • Execute updateproject.bat to update BBDD between versions
  • Execute bootstraptest.bat to test the final resulCode First …

The createproject script does the following:

php doctrinecli.php orm:schema-tool:drop --force
rm -rf entities
php doctrinecliyaml.php orm:generate-entities . --generate-annotations="TRUE"
cd entities
sed -i 's/@ORM\\/@/g' *.php
sed -i 's/NONE/AUTO/g' *.php
cd ..
php doctrinecli.php orm:generate-entities . --generate-annotations="TRUE"
php doctrinecli.php orm:schema-tool:create
mv Entities tmpEntities
mv tmpEntities entities
  • it drops the database if exists, and remove the entities directory
  • generate the php classes from the YAML model definitions
  • Really Ugly Kludge, removing ORM prefix from classes
  • generate getters and setters in .php classes
  • generate the database tables
  • rename entities directory to keep a good eye lowercasecamel

The Doctrine 2.2.1 Bootstrap sequence is:


$config->setAutoGenerateProxyClasses(($APPLICATION_ENV == “development”));

// Mapping Configuration (4)
//$driverImpl = new Doctrine\ORM\Mapping\Driver\XmlDriver(__DIR__.”/config/mappings/xml”);
//$driverImpl = new Doctrine\ORM\Mapping\Driver\YamlDriver(__DIR__.”/Models”);
$driverImpl = $config->newDefaultAnnotationDriver(__DIR__.”/entities”);

// Caching Configuration (5)
if ($APPLICATION_ENV == “development”) {
$cache = new \Doctrine\Common\Cache\ArrayCache();
} else {
$cache = new \Doctrine\Common\Cache\ApcCache();

// database configuration parameters (6)
$conn = array(
‘driver’ => ‘pdo_sqlite’,
‘path’ => __DIR__ . ‘/database.sqlite’,

// obtaining the entity manager (7)
$evm = new Doctrine\Common\EventManager();
$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);

The database connections are defined at step 6. Also, at step 4 you can choose the doctrine mapping configuration.

Code First …

Code First Approach involves creating models before database. So, in a real project, the steps to use this thing are:

  • Download, install & configure.
    useYAML to write your Entities.*.dcm.yml classes representing the model.
  • Execute createproject.bat to create your php.classes (may need a manual touch) and create a database schema according to your model.

A final thought …

I am doing an ugly kludge in createproject.bat, using sed to eliminate the ORM prefix. I’v tried putting “as ORM” in the php classes header too but don’t remember well, it didn’t worked , so i did this hack

4 thoughts on “Code First Approaching PHP with Doctrine 2.2.1 and Composer

  1. Pingback: Manu º¹ºº¹¹º¹º¹¹¨¨

  2. Pingback: Manu º¹ºº¹¹º¹º¹¹¨¨

  3. Pingback: Manu º¹ºº¹¹º¹º¹¹¨¨

  4. Pingback: Manu º¹ºº¹¹º¹º¹¹¨¨

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>