Skip to main content

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.

Comments

Popular posts from this blog

AngularJs Tutorial: Getting Started

This tutorial will help you to create a dynamic web application using power of AngularJs. ·   What is AngularJs AngularJs is javascript MVC framework (maintained by Google) which molds static HTML application to dynamic web application. That was a brief introduction, you can find more information on Wiki and AngularJS Site . AngularJs follows MVC pattern to separate out model from view and manage it by controller. See this image Goal of using AngularJs is to convert static HTML template to dynamic (Angular) template.   ·   How to convert static template to dynamic (Angular) Template Now consider the following static HTML template which contain list of movies title. <html> <head></head> <body>   <h1>Upcoming Movies</h1> <ul>     <li>       <span>Godzilla</span>     </li> <li>       <span>Batman vs Superman</span>     </li> <li>       <span>Sta

AngularJs Tutorials : Bootstrap

Every application starts with bootstrap process which initialize application and wire it other with dependencies and configurations. AngularJs is not different from other application. It also starts application with bootstrap process. Following operation happens inside AngularJs bootstrap process. Load application module. Create dependency injector and load dependencies. Compile HTML and create scope for application. All these steps happens inside angular.js scripting file. therefore we need to include it first. we can include it inside HEAD tag or at end of BODY tag. Note: Adding angular.js file at end of body tag will allow browser to load of HTML elements without any delay and afterwards load angular.js and begin bootstrapping process. You can get angular.js source file from https://code.angularjs.org/ <script src= ”https://code.angularjs.org/1.3.0/angular.js” type= ”text/javascript” > AngularJs bootstrap process happens on docum

How to create SSH Key for GitHub and Bitbucket

This  post  demonstrate how to  generate  SSH private and  public   key  with fundamental steps and  assist   you  to  add   public   key  inside your  GitHub  and Bitbucket account. But first why we  need  this SSH  public   key ? Whether you are working in the team or an individual and you want make any operations on secure GIT repository like clone or push your latest changes then you require to provide your credentials to help GIT to authenticate you and approve those operations. But when  you  do so,  you  are  asked  to  provide  your username &  password ,  each   time  on  each   operation . It looks  simple  earlier, but later  becomes  a  headache . SSH  connection   helps   us  to  authenticate  using public and private keys, that  we  generate only once, later  we  don’t need to  authenticate ,  SSH  will  do  this behalf of  us . But how does it  work ? SSH   authenticate  you  using  an  identity , your  identity  and this  identity   comprise   combination  of p