I have downloaded doctrine 2.2 orm. I have gone through its installation guide but i cant properly understand its documentation. Can someone guide me through the setup process of doctrine. I have previously been working with Hibernate ORM framework in java. They have excellent documentation which are easy to understand for beginners. I dont find the documentation of doctrine to be of that level. Can somebody provide some sample projects on doctrine to begin with?
There are several ways on how to install doctrine to your website project. I will show you an easy alternative:
Download doctrine package and uncompressed inin your server. Now your directory looks like this:
localhost/Doctrine
localhost/Doctrine/Common
localhost/Doctrine/ORM
localhost/Doctrine/DBAL
You need to create two additionals folder in order to store your models (persistence entities), and proxies:
localhost/models
localhost/proxies
Create a class that will in charge of create the EntityManager object and the connection to database. Lets create the magic class named Doctrine:
localhost/doctrine.php
Setting the properties:
<?php
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSQLLogger;
class Doctrine{
public $em = null;
public function __construct()
{
require_once 'Doctrine/Common/ClassLoader.php';
$doctrineClassLoader = new ClassLoader('Doctrine', '/');
$doctrineClassLoader->register();
$entitiesClassLoader = new ClassLoader('models', '/models/');
$entitiesClassLoader->register();
$proxiesClassLoader = new ClassLoader('Proxies', '/proxies/');
$proxiesClassLoader->register();
// Set up caches
$config = new Configuration;
$cache = new ArrayCache;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(array('/models/Entities'));
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// Proxy configuration
$config->setProxyDir('/proxies');
$config->setProxyNamespace('Proxies');
// Set up logger
$logger = new EchoSQLLogger;
//$config->setSQLLogger($logger);
$config->setAutoGenerateProxyClasses( TRUE );
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => 'USER',
'password' => 'PASS',
'host' => 'HOST',
'dbname' => 'DB_NAME'
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);
}
}
Now your able to use the entityManager in you website once you have included it.
$doctrine = new Doctrine();
$user = new models\User;
$doctrine->em->persist($user);
$doctrine->em->flush();
Al least this post can help you to take the idea on how to install and use doctrine
Dotrine 2.5.4
Using namespace
namespace DB;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
class Doctrine
{
public $em;
public function __construct()
{
$paths = array("src/Application/Models/Entity");
$isDevMode = false;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'USER',
'password' => 'PASS',
'host' => 'HOST',
'dbname' => 'DB_NAME',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
$this->em = EntityManager::create($dbParams, $config);
}
}
Create Article entity in src\Application\Models\Entity
namespace Application\Models\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="article")
* @ORM\Entity(repositoryClass="Application\Models\Entity\ArticleRepository")
*/
class Article
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @var string $title
*
* @ORM\Column(type="text", length=255, nullable=true)
*/
protected $title;
/**
* @var text $body
*
* @ORM\Column(type="text", nullable=true)
*/
protected $body;
/**
* @var datetime $createdAt
*
* @ORM\Column(type="datetime", name="created_at")
*/
private $createdAt;
Now call the entity manager from your classes:
$doctrine = new Doctrine();
$article = $doctrine->em->find('Application\Models\Entity\Article', 1);