Thursday, October 23, 2014

Doctrine 2 Tutorials: Installation and Configuration



This tutorial post will help you to install Doctrine2 inside your PHP project and configure it so that you can use it's features in plain PHP project.

This post illustrates installation of Doctrine2 using Composer.

Installation

First create your project directory


 mkdir zainabed   

 cd zainabed

now create composer.json


vi composer.json

then add following repository information.


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

Now you are ready to install it via composer, but first we need to install composer into you machine.


curl -sS https://getcomposer.org/installer | php

Then initiate following command to install Doctrine2


php composer.phar install

composer creates "autoload.php" file which helps you to autoload all PHP classes of Doctrine2 ORM project.

Configuration

First create configuration file configuration.php for Doctrine2 and include autoload.php inside it.


<?php

// configuration.php

// Include Composer Autoload 
require_once "vendor/autoload.php";

Then create database configuration details, like database name, username ,password.


// database configuration

$databaseParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'zainabed',
);

Then specify Entity path where you want to store all ORM Entities.


//entity path
$entityPath = array("src/Entity");

Now create Entity Manager object.
Entity Manager is center source of ORM which handle interaction between Entities and Database.


//annotation configuration
$config = Setup::createAnnotationMetadataConfiguration($entityPath, false);

//entity manager object
$entityManager = EntityManager::create($databaseParams, $config);

now you are ready to use Doctrine2 inside your PHP project.

following is complete configuration example which also includes configuration based on XML and YML.


<?php

// configuration.php

// Include Composer Autoload 
require_once "vendor/autoload.php";

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

// database configuration
$databaseParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'zainabed',
);

//entity path
$entityPath = array("src/Entity");


//annotation configuration
$config = Setup::createAnnotationMetadataConfiguration($entityPath, false);

//entity manager object
$entityManager = EntityManager::create($databaseParams, $config);

//xml configuration
//$xmlEntituPath = array("/path/to/xml-mappings");
//$config = Setup::createXMLMetadataConfiguration($xmlEntituPath, false);
//$entityManager = EntityManager::create($databaseParams, $config);

//yml configuration
//$ymlEntityPath = array("/path/to/yml-mappings");
//$config = Setup::createYAMLMetadataConfiguration($ymlEntityPath, false);
//$entityManager = EntityManager::create($databaseParams, $config);

Doctrine 2 is now configured and ready to use.

Zainul Abedin Shaikh

Founder of Zainabed, Passionate about programming. Follow him on Twitter

And if you enjoyed this post, please consider sharing it on Facebook, Google+ or Twitter below