Doctrine – a very famous and strong ORM library for PHP web app development now can be easily integrated into CodeIgniter. (this post is assumed that you already know about PHP, CodeIgniter, ORM, Doctrine… so I just do the rest: to integrate them together)
1. Create CodeIgniter application ready to integrate Doctrine
You will need a folder name codeigniter-doctrine and copy the codeigniter application, system and index.php file into it. The result should look like this.
2. Download Doctrine library for CodeIgniter
Just download Doctrine ORM from it homepage, extract it and you will see the following:
We will need that Doctrine lib folder, copy it into CodeIgniter application libraries folder, and create a file name Doctrine.php just like this.
3. Doctrine and CodeIgniter Integration
Now you’re ready to configure the Doctrine library for CodeIgniter, open the file Doctrine.php in your text editor and replace it with the following content:
<?php
use DoctrineCommonClassLoader,
DoctrineORMConfiguration,
DoctrineORMEntityManager,
DoctrineCommonCacheArrayCache,
DoctrineDBALLoggingEchoSQLLogger;
class Doctrine {
public $em = null;
public function __construct()
{
// load database configuration from CodeIgniter
require_once APPPATH.'config/database.php';
// Set up class loading. You could use different autoloaders, provided by your favorite framework,
// if you want to.
require_once APPPATH.'libraries/Doctrine/Common/ClassLoader.php';
$doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'libraries');
$doctrineClassLoader->register();
$entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
$entitiesClassLoader->register();
$proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
$proxiesClassLoader->register();
// Set up caches
$config = new Configuration;
$cache = new ArrayCache;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models/Entities'));
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// Proxy configuration
$config->setProxyDir(APPPATH.'/models/proxies');
$config->setProxyNamespace('Proxies');
$config->setAutoGenerateProxyClasses( TRUE );
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => 'webapp',
'password' => 'webapp',
'host' => 'localhost',
'dbname' => 'mydb'
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);
}
}
As you can see from the above, we do most Doctrine config in this file, please change the $connectionOptions to the right database information you’re using.
You also should change the autoload config of CodeIgniter to load Doctrine library ready for each action. Find /application/config/autoload.php and change as following:
OK you’re good now.
4. Testing CodeIgniter-Doctrine Integration
I will use the blogging example, but just with a simple BLOG table for demonstration with the integration.
Use MySQL workbench to create the following table + data:
CREATE TABLE `blog` ( `ID` int(11) NOT NULL AUTO_INCREMENT , `CONTENT` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `POSTED_BY` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `POSTED_DATE` datetime NULL DEFAULT NULL , `TITLE` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=5 ;
OK so now you got the table. Put some content to it, whatever you want, just give it some dummy content.
We will now make a Doctrine model class, in the models folder of CodeIgniter Doctrine blog application, create a file name: Blog.php
Here is its content:
<?php
/**
* @Entity @Table(name="blog")
*/
class Blog
{
/**
* @Id @GeneratedValue @Column(type="integer")
* @var string
*/
protected $id;
/**
* @Column(type="string")
* @var string
*/
public $title;
/**
* @Column(type="string")
* @var string
*/
public $content;
public function getId()
{
return $this->id;
}
public function getContent()
{
return $this->content;
}
public function setContent($content)
{
$this->content = $content;
}
public function getTitle() {
return $this->title;
}
public function setTitle($title) {
$this->title = $title;
}
}
Now to do something, we must create a blog controller, create a file name blogcontroller.php in the folder /application/controllers with the following content:
<?php
require_once APPPATH . 'models/Blog.php';
class blogcontroller extends CI_Controller{
var $em;
function __construct() {
parent::__construct();
$this->em = $this->doctrine->em;
}
public function index()
{
$blog = $this->em->find('Blog', 1);
echo $blog->title . '<br/>' . $blog->content;
}
}
Can you see I import the Blog.php file to this controller so we can use the Blog class? Yes, we must do so in order the entity manager can understand which entity class to interact with.
You can also see that when I construct the controller, I also get $this->doctrine->em, this is the entity manager we’ve configured so far in step 3 just now you use it.
Finally, I just find a Blog object with id = 1 to print out its title and its content. It’s just want to keep it so simple to demonstrate if the integration work.
Can you guess the result? Here it is on my browser:
5. CodeIgniter Doctrine Integration Zip
- sample blog db sql file
- codeigniter-doctrine-integration-zip
That’s it! have a good day!
this error :
Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or ‘(‘ in C:\AppServ\www\codeigniter2-doctrine2\application\libraries\Doctrine.php on line 2
It works!