integrating-with-codeigniter.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. Integrating with CodeIgniter
  2. ============================
  3. This is recipe for using Doctrine 2 in your
  4. `CodeIgniter <http://www.codeigniter.com>`_ framework.
  5. .. note::
  6. This might not work for all CodeIgniter versions and may require
  7. slight adjustments.
  8. Here is how to set it up:
  9. Make a CodeIgniter library that is both a wrapper and a bootstrap
  10. for Doctrine 2.
  11. Setting up the file structure
  12. -----------------------------
  13. Here are the steps:
  14. - Add a php file to your system/application/libraries folder
  15. called Doctrine.php. This is going to be your wrapper/bootstrap for
  16. the D2 entity manager.
  17. - Put the Doctrine folder (the one that contains Common, DBAL, and
  18. ORM) inside that same libraries folder.
  19. - Your system/application/libraries folder now looks like this:
  20. system/applications/libraries -Doctrine -Doctrine.php -index.html
  21. - If you want, open your config/autoload.php file and autoload
  22. your Doctrine library.
  23. <?php $autoload['libraries'] = array('doctrine');
  24. Creating your Doctrine CodeIgniter library
  25. ------------------------------------------
  26. Now, here is what your Doctrine.php file should look like.
  27. Customize it to your needs.
  28. .. code-block:: php
  29. <?php
  30. use Doctrine\Common\ClassLoader,
  31. Doctrine\ORM\Configuration,
  32. Doctrine\ORM\EntityManager,
  33. Doctrine\Common\Cache\ArrayCache,
  34. Doctrine\DBAL\Logging\EchoSQLLogger;
  35. class Doctrine {
  36. public $em = null;
  37. public function __construct()
  38. {
  39. // load database configuration from CodeIgniter
  40. require_once APPPATH.'config/database.php';
  41. // Set up class loading. You could use different autoloaders, provided by your favorite framework,
  42. // if you want to.
  43. require_once APPPATH.'libraries/Doctrine/Common/ClassLoader.php';
  44. $doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'libraries');
  45. $doctrineClassLoader->register();
  46. $entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
  47. $entitiesClassLoader->register();
  48. $proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
  49. $proxiesClassLoader->register();
  50. // Set up caches
  51. $config = new Configuration;
  52. $cache = new ArrayCache;
  53. $config->setMetadataCacheImpl($cache);
  54. $driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models/Entities'));
  55. $config->setMetadataDriverImpl($driverImpl);
  56. $config->setQueryCacheImpl($cache);
  57. $config->setQueryCacheImpl($cache);
  58. // Proxy configuration
  59. $config->setProxyDir(APPPATH.'/models/proxies');
  60. $config->setProxyNamespace('Proxies');
  61. // Set up logger
  62. $logger = new EchoSQLLogger;
  63. $config->setSQLLogger($logger);
  64. $config->setAutoGenerateProxyClasses( TRUE );
  65. // Database connection information
  66. $connectionOptions = array(
  67. 'driver' => 'pdo_mysql',
  68. 'user' => $db['default']['username'],
  69. 'password' => $db['default']['password'],
  70. 'host' => $db['default']['hostname'],
  71. 'dbname' => $db['default']['database']
  72. );
  73. // Create EntityManager
  74. $this->em = EntityManager::create($connectionOptions, $config);
  75. }
  76. }
  77. Please note that this is a development configuration; for a
  78. production system you'll want to use a real caching system like
  79. APC, get rid of EchoSqlLogger, and turn off
  80. autoGenerateProxyClasses.
  81. For more details, consult the
  82. `Doctrine 2 Configuration documentation <http://www.doctrine-project.org/documentation/manual/2_0/en/configuration#configuration-options>`_.
  83. Now to use it
  84. -------------
  85. Whenever you need a reference to the entity manager inside one of
  86. your controllers, views, or models you can do this:
  87. .. code-block:: php
  88. <?php
  89. $em = $this->doctrine->em;
  90. That's all there is to it. Once you get the reference to your
  91. EntityManager do your Doctrine 2.0 voodoo as normal.
  92. Note: If you do not choose to autoload the Doctrine library, you
  93. will need to put this line before you get a reference to it:
  94. .. code-block:: php
  95. <?php
  96. $this->load->library('doctrine');
  97. Good luck!