Doctrine Integration with CodeIgniter

I have successfully installed the latest version of CodeIgniter and have a basic MVC pattern. The problem I noticed is that CI, of course, doesn't allow you to prepare statements when it comes to queries. So, I decided to download Doctrine 1 from GitHub . I am very new to Doctrine and need some help integrating it with CI, so I followed this tutorial .

In one of my controllers I have

$this->load->library('doctrine'); $this->em = $this->doctrine->em; 

But, when I go to load the view in my browser, I am greeted by a read error

Message: require_once (/Applications/MAMP/htdocs/CodeIgniter/application/libraries/Doctrine/Common/ClassLoader.php): could not open the stream: there is no such file or directory

When further checking Doctrine downloads from GitHub, there’s not even a folder called "common" anywhere. I am very new to CI and especially to doctrine. Does anyone have any tips that can help me get this to work? Also, is it possible to use the MySQLi driver instead of PDO with Doctrine?

+6
source share
7 answers

Downloading ORM doctrines directly from GitHub does not include other dependencies. They are managed by Composer . If you look into the composer.json file, you will see these dependencies. If you want to install them manually, this is:

  • doctrine / general
  • doctrine / inflector
  • doctrine / cache
  • doctrine / collections
  • doctrines / lexer
  • doctrine / annotations
  • doctrine / DBAL
  • Symfony / console

I believe all of them. You will have to merge these files into the appropriate directories, following the PSR-0 standards for autoload classes.

Alternatively, install Doctrine 2 with Composer with the following composer.json file, and any other dependencies will be installed automatically. Then integrates with CodeIgniter .

 { "minimum-stability": "stable", "require": { "doctrine/orm": "2.3.*" } } 

Modify the index.php file of your CodeIgniter application by adding one line to include the autoloader file before requiring the CodeIgniter kernel.

 require_once BASEPATH.'../vendor/autoload.php'; require_once BASEPATH.'core/CodeIgniter.php'; 

Also, if you are installing with Composer, use this edited version of bootstrap as the contents of application/libraries/Doctrine.php , which worked for me

 <?php use Doctrine\Common\ClassLoader, Doctrine\ORM\Tools\Setup, Doctrine\ORM\EntityManager; class Doctrine { public $em; public function __construct() { // Load the database configuration from CodeIgniter require APPPATH . 'config/database.php'; $connection_options = array( 'driver' => 'pdo_mysql', 'user' => $db['default']['username'], 'password' => $db['default']['password'], 'host' => $db['default']['hostname'], 'dbname' => $db['default']['database'], 'charset' => $db['default']['char_set'], 'driverOptions' => array( 'charset' => $db['default']['char_set'], ), ); // With this configuration, your model files need to be in application/models/Entity // eg Creating a new Entity\User loads the class from application/models/Entity/User.php $models_namespace = 'Entity'; $models_path = APPPATH . 'models'; $proxies_dir = APPPATH . 'models/Proxies'; $metadata_paths = array(APPPATH . 'models'); // Set $dev_mode to TRUE to disable caching while you develop $config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode = true, $proxies_dir); $this->em = EntityManager::create($connection_options, $config); $loader = new ClassLoader($models_namespace, $models_path); $loader->register(); } } 

Note. Version 3 of CodeIgniter will be installed with Composer upon release, but version 2 will not.

+12
source

For those looking for a tutorial on integrating Doctrine 2 with CodeIgniter, this question and other answers are deprecated (for CI 2). This is the new tutorial for CI 3 that I did and I checked the work:

How to install Doctrine 2 in CodeIgniter 3


I repeat it here.

Install Doctrine

ORMs Doctrine 2 Documentation - Installation and Setup

Doctrine can be established with Composer . Define the following requirement in the composer.json file:

 { "require": { "doctrine/orm": "*" } } 

Then call the composition from the command line.

Integration with CodeIgniter

Doctrine 2 ORMs Documentation - Integration with CodeIgniter

Here are the steps: Add the php file to the system / application / libraries folder called Doctrine.php. This will be your wrapper / bootstrap for the D2 entity manager. Place the Doctrine folder (the one that contains Common, DBAL, and ORM) inside the third_party folder. If you want, open the config / autoload.php file and load your Doctrine library: $autoload['libraries'] = array('doctrine');

Creating the Doctrine CodeIgniter Library

Now, here is what your Doctrine.php file should look like. Customize it according to your needs.

 <?php /** * Doctrine 2.4 bootstrap * */ 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'; // include Doctrine ClassLoader class require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php'; // load the Doctrine classes $doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'third_party'); $doctrineClassLoader->register(); // load the entities $entityClassLoader = new ClassLoader('Entities', APPPATH.'models'); $entityClassLoader->register(); // load the proxy entities $proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies'); $proxiesClassLoader->register(); // load Symfony2 classes // this is necessary for YAML mapping files and for Command Line Interface (cli-doctrine.php) $symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine'); $symfonyClassLoader->register(); // Set up the configuration $config = new Configuration; // Set up caches if(ENVIRONMENT == 'development') // set environment in index.php // set up simple array caching for development mode $cache = new \Doctrine\Common\Cache\ArrayCache; else // set up caching with APC for production mode $cache = new \Doctrine\Common\Cache\ApcCache; $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); // set up annotation driver $driver = new \Doctrine\ORM\Mapping\Driver\PHPDriver(APPPATH.'models/Mappings'); $config->setMetadataDriverImpl($driver); // Proxy configuration $config->setProxyDir(APPPATH.'/models/Proxies'); $config->setProxyNamespace('Proxies'); // Set up logger $logger = new EchoSQLLogger; $config->setSQLLogger($logger); $config->setAutoGenerateProxyClasses( TRUE ); // only for development // 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, and store it for use in our CodeIgniter controllers $this->em = EntityManager::create($connectionOptions, $config); } } 

Command line tool setup

Doctrine comes with several command line tools that are very useful during development.

Check if these lines exist in the Doctrine.php file to load Symfony classes to use command line tools (and for YAML mapping files):

 $symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine'); $symfonyClassLoader->register(); 

You need to register your EntityManager applications in the console tool in order to use the tasks by creating the cli-doctrine.php file in the application directory with the following contents:

  <?php /** * Doctrine CLI bootstrap for CodeIgniter * */ define('APPPATH', dirname(__FILE__) . '/'); define('BASEPATH', APPPATH . '/../system/'); define('ENVIRONMENT', 'development'); require APPPATH.'libraries/Doctrine.php'; $doctrine = new Doctrine; $em = $doctrine->em; $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array( 'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()), 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em) )); \Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet); ?> 

Now run this script through the PHP command line and you will see a list of commands available to you.

 php cli-doctrine.php 

Creating display classes from the database:

 php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities 

if you get this error: Fatal error: call to undefined function Doctrine \ Common \ Cache \ apc_fetch () install the APC extension for PHP:

 sudo apt-get install php-apc sudo /etc/init.d/apache2 restart 

For production mode, you'll want to use a real caching system like APC, get rid of EchoSqlLogger and disable autoGenerateProxyClasses in Doctrine.php

+2
source

Find the link to integrate doctrine into CI https://github.com/mitul69/codeigniter-doctrine-integration

+1
source

Note that code igniter 2 has a slight difference in the organization of the code. In code igniter 2, it’s best to place the Doctrine folder in the application/third_party folder instead of the application/libraries folder (otherwise it won’t work!).

Learn more about this here.

0
source

I had the same problem when I tried to follow this guide from the doctrine user guide http://doctrine-orm.readthedocs.org/en/latest/cookbook/integrating-with-codeigniter.html

This problem occurs when I tried to install through the composer, so I went to this website http://www.doctrine-project.org/downloads/ and manually download the version of DoctrineORM-2.3.3-full.tar.gz, and the error has disappeared.

0
source

The original problem with the poster seems to be a problem at startup. I was given a similar problem when trying to configure CodeIgniter and Doctrine with Composer. In CodeIgniter 3, you can enable composer autoload, which should allow you to properly download all Doctrine files. For this to work, specify the Composer provider provider for the application / provider. You can also do this in older versions, but then you must manually include the Composer startup file in your Doctrine library file for CodeIgniter. If you need more information: I wrote a blog post that describes how to do this. http://blog.beheist.com/integrating-codeigniter-and-doctrine-2-orm-with-composer/

0
source

you can use this

through composer: composer create-project rbz / codeigniter your project

via git: git clone https://github.com/dandisy/cihmvctwig.git

0
source

Source: https://habr.com/ru/post/947358/


All Articles