Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Created June 3, 2014 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseluisq/b4c1f6434315352d3f8c to your computer and use it in GitHub Desktop.
Save joseluisq/b4c1f6434315352d3f8c to your computer and use it in GitHub Desktop.
Working with Doctrine 2 in CodeIgniter PHP Framework

Doctrine for Codeigniter

Working with Doctrine in CodeIgniter PHP Framework

// On 'your_controller.php' file

// Eg. I'm using an entity called 'User' (replace with your own entity class)
include_once '/application/models/User.php';

// Get EntityManager Instance
$em = $this->doctrine->getEntityManager();

// My entity User for insert
$user = new User();
$user->setUserName('Your Name');
$user->setUserEmail('your@email.com');
$em->persist($user);
$em->flush();
<?php
// Add 'Doctrine' library to auto-load
$autoload['libraries'] = array(
'session',
'database',
'Doctrine'
);
<?php
/**
* Doctrine boostrap file for CodeIgniter
*/
use Doctrine\Common\ClassLoader,
Doctrine\Common\Cache\ArrayCache,
Doctrine\Common\Annotations\AnnotationRegistry,
Doctrine\Common\Annotations\AnnotationReader,
Doctrine\ORM\Tools\Setup,
Doctrine\ORM\EntityManager,
Doctrine\ORM\Mapping\Driver\AnnotationDriver;
$PATH_APP = dirname(__DIR__);
$PATH_DOCTRINE = 'Doctrine';
include $PATH_APP . '/config/database.php';
require_once $PATH_DOCTRINE . '/Common/ClassLoader.php';
$doctrineClassLoader = new ClassLoader('Doctrine', __DIR__);
$doctrineClassLoader->register();
$applicationMode = 'development';
$isDevMode = FALSE;
$paths = array(APPPATH . '/models');
$cacheImpl = $applicationMode == 'development' ? new ArrayCache : new Doctrine\Common\Cache\ApcCache;
// Configuration
$config = Setup::createConfiguration($isDevMode);
$driver = new AnnotationDriver(new AnnotationReader(), $paths);
// Registering noop annotation autoloader - allow all annotations by default
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);
$config->setMetadataCacheImpl($cacheImpl);
$config->setQueryCacheImpl($cacheImpl);
$conn = array(
'host' => $db['default']['hostname'],
'driver' => 'pdo_' . $db['default']['dbdriver'],
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'dbname' => $db['default']['database'],
'port' => $db['default']['port'],
'charset' => $db['default']['char_set'],
);
$entityManager = EntityManager::create($conn, $config);
<?php
/**
* Doctrine library for CodeIgniter
*/
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
class Doctrine {
/**
* @var EntityManager Doctrine Entity Manager
*/
private $entityManager = NULL;
public function __construct() {
require_once 'bootstrap.php';
$this->entityManager = EntityManager::create($conn, $config);
}
/**
* @return EntityManager Doctrine Entity Manager
*/
public function getEntityManager() {
return $this->entityManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment