Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:46
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 doctrinebot/bcc064b9900f16010b5e to your computer and use it in GitHub Desktop.
Save doctrinebot/bcc064b9900f16010b5e to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-489 - https://github.com/doctrine/doctrine2/issues/4993
Index: en.txt
===================================================================
--- en.txt (revision 7520)
+++ en.txt (working copy)
@@ -1,5 +1,6 @@
-
-+ Implementing ArrayAccess for domain objects
-+ Implementing the NOTIFY changetracking policy
-+ Validation of Entities
-+ Implementing wakeup or clone
\ No newline at end of file
+
++ Implementing ArrayAccess for domain objects
++ Implementing the NOTIFY changetracking policy
++ Validation of Entities
++ Implementing wakeup or clone
++ Integrating with CodeIgniter
\ No newline at end of file
Index: en/integrating-with-codeigniter.txt
===================================================================
--- en/integrating-with-codeigniter.txt (revision 0)
+++ en/integrating-with-codeigniter.txt (revision 0)
@@ -0,0 +1,106 @@
+This is recipe for using Doctrine 2 in your [CodeIgniter](http://www.codeigniter.com) framework.
+
+Here is how to set it up:
+
+Make a CodeIgniter library that is both a wrapper and a bootstrap for Doctrine 2.
+
+++ Setting up the file structure
+
+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');
+
+++ Creating your Doctrine CodeIgniter library
+
+Now, here is what your Doctrine.php file should look like. Customize it to your needs.
+
+[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()
+ {
+ // 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);
+ $config->setQueryCacheImpl($cache);
+
+ // Proxy configuration
+ $config->setProxyDir(APPPATH.'/models/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' => $db['default']['username'],
+ 'password' => $db['default']['password'],
+ 'host' => $db['default']['hostname'],
+ 'dbname' => $db['default']['database']
+ );
+
+ // 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).
+
+++ Now to use it
+
+Whenever you need a reference to the entity manager inside one of your controllers, views, or models you can do this:
+
+[php]
+$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:
+
+[php]
+$this->load->library('doctrine');
+
+Good luck!
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment