1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/docs/en/cookbook/integrating-with-codeigniter.rst

141 lines
4.2 KiB
ReStructuredText
Raw Normal View History

Integrating with CodeIgniter
============================
This is recipe for using Doctrine 2 in your
`CodeIgniter <http://www.codeigniter.com>`_ framework.
.. note::
This might not work for all CodeIgniter versions and may require
slight adjustments.
2010-04-06 22:36:40 +04:00
2010-04-06 22:36:40 +04:00
Here is how to set it up:
Make a CodeIgniter library that is both a wrapper and a bootstrap
for Doctrine 2.
2010-04-06 22:36:40 +04:00
Setting up the file structure
-----------------------------
2010-04-06 22:36:40 +04:00
Here are the steps:
- Add a php file to your system/application/libraries folder
called Doctrine.php. This is going to be your wrapper/bootstrap for
the D2 entity manager.
- Put the Doctrine folder (the one that contains Common, DBAL, and
ORM) inside that same libraries folder.
- Your system/application/libraries folder now looks like this:
system/applications/libraries -Doctrine -Doctrine.php -index.html
- If you want, open your config/autoload.php file and autoload
your Doctrine library.
<?php $autoload['libraries'] = array('doctrine');
2010-04-06 22:36:40 +04:00
Creating your Doctrine CodeIgniter library
------------------------------------------
2010-04-06 22:36:40 +04:00
Now, here is what your Doctrine.php file should look like.
Customize it to your needs.
2010-04-06 22:36:40 +04:00
.. code-block:: php
2010-04-06 22:36:40 +04:00
<?php
2010-04-06 22:36:40 +04:00
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
2011-02-01 00:46:41 +03:00
Doctrine\DBAL\Logging\EchoSQLLogger;
2010-04-06 22:36:40 +04:00
class Doctrine {
2010-04-06 22:36:40 +04:00
public $em = null;
2010-04-06 22:36:40 +04:00
public function __construct()
{
// load database configuration from CodeIgniter
require_once APPPATH.'config/database.php';
2010-04-06 22:36:40 +04:00
// 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';
2010-04-06 22:36:40 +04:00
$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();
2010-04-06 22:36:40 +04:00
// Set up caches
$config = new Configuration;
$cache = new ArrayCache;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models/Entities'));
$config->setMetadataDriverImpl($driverImpl);
2010-04-06 22:36:40 +04:00
$config->setQueryCacheImpl($cache);
$config->setQueryCacheImpl($cache);
2010-04-06 22:36:40 +04:00
// Proxy configuration
$config->setProxyDir(APPPATH.'/models/proxies');
$config->setProxyNamespace('Proxies');
2010-04-06 22:36:40 +04:00
// Set up logger
2011-02-01 00:46:41 +03:00
$logger = new EchoSQLLogger;
$config->setSQLLogger($logger);
2010-04-06 22:36:40 +04:00
$config->setAutoGenerateProxyClasses( TRUE );
2010-04-06 22:36:40 +04:00
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
2010-04-06 22:36:40 +04:00
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);
}
}
Please note that this is a development configuration; for a
production system you'll want to use a real caching system like
APC, get rid of EchoSqlLogger, and turn off
autoGenerateProxyClasses.
For more details, consult the
`Doctrine 2 Configuration documentation <http://www.doctrine-project.org/documentation/manual/2_0/en/configuration#configuration-options>`_.
2010-04-06 22:36:40 +04:00
Now to use it
-------------
2010-04-06 22:36:40 +04:00
Whenever you need a reference to the entity manager inside one of
your controllers, views, or models you can do this:
2010-04-06 22:36:40 +04:00
.. code-block:: php
2010-04-06 22:36:40 +04:00
<?php
2010-04-06 22:36:40 +04:00
$em = $this->doctrine->em;
That's all there is to it. Once you get the reference to your
EntityManager do your Doctrine 2.0 voodoo as normal.
Note: If you do not choose to autoload the Doctrine library, you
will need to put this line before you get a reference to it:
2010-04-06 22:36:40 +04:00
.. code-block:: php
2010-04-06 22:36:40 +04:00
<?php
2010-04-06 22:36:40 +04:00
$this->load->library('doctrine');
Good luck!