Install Doctrine2 using Composer

Doctrine2 is one of the top PHP 5.2+ ORM, or rather, it is an Object Document Mapper (OrientDB), Database Abstraction Layer, Migration tool, Object Relational Mapper. I most frequently use Doctrine2 with Symfony2 but it’s possible to use it without Symfony2. I would like to show how you can install it and how you can configure its command line tool.

So, I used Composer to install Doctrine2. I created the new directory and installed the Composer.

 mkdir test_doctrine  
 cd test_directory  
 curl -s https://getcomposer.org/installer | php  

then I created composer.json file:

 {  
     "require": {  
         "doctrine/orm": "*"  
     }  
 }  

After that you can install the Doctrine2 you just use command:

 php composer.phar install  

This command will create directory “vendor” and it should install all Doctrine2 component as well as the Symfony2 command line component.

If you need Doctrine2 command line tool then you have to create “cli-config.php” into your root directory.

cli-config.php

 <?php  
 require_once "vendor/autoload.php";  

 use Doctrine\ORM\Tools\Setup;  
 use Doctrine\ORM\EntityManager;  

 $paths = array("Entities");  
 $isDevMode = false;  

 // the connection configuration  
 $dbParams = array(  
   'driver'  => 'pdo_mysql',  
   'user'   => 'username',  
   'password' => 'password',  
   'dbname'  => 'testdatabase',  
 );  

 // Any way to access the EntityManager from your application  
 $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);  
 $em = EntityManager::create($dbParams, $config);  

 $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(  
   'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),  
   'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)  
 ));  

..and you should create your model directory:

 mkdir Entities  

After that you can generate your first entity classes with command:

 ./vendor/bin/doctrine orm:convert-mapping --from-database annotation Entities  

The last parameter is your entity DIR name.

Doctrine2 is a complex ORM but I think you should use it in your no-frameworks projects because it has very good generators and cache system.

If you want to know more information from Doctrine2 configuration you should look this URL: http://docs.doctrine-project.org/en/latest/reference/configuration.html

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Install Doctrine2 using Composer

  1. Anonym says:

    After mkdir absolutely nothing of this works as described..

    • janosvajda says:

      Hi, I am sorry for the late answer, but I did not edit this blog in the last 4 years.

      I just opened it today. To be honest, I wrote this article in 2013, and it worked as I had tested it. I have no idea why does not work for you. Sorry for this. 🙂

Leave a comment