How to install Doctrine in CodeIgniter 3

The official guide is not complete, while others are for CI2.

Therefore, I give you a textbook, which I checked myself, works.

I know that SO encourage users to answer their questions .

+9
source share
2 answers

Set Doctrine

(The following instructions have been changed from: ORM Documentation Doctrine 2 - Installation and Setup )

Doctrine can be installed using Composer :

  1. At the command prompt (for example, Windows: Start> cmd), go to the folder where the files should be installed (for example, htdocs / my_project).

  2. Use Composer to install files:

    a. run C:\>composer install doctrine/orm

    or:

    b. Define the following requirement in your composer.json file:

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

    and then call composer install from the command line.

Composer will install vendor folders with numerous subfolders and several hundred php files. Move this vendor folder to the CodeIgniter application tree. For simplicity, you can put this here:

 /application /config /controllers /libraries Doctrine.php <-- the doctrine bootstrap/wrapper file /third_party /vendor <-- the folder installed by Composer. don't touch the files or folders below it -- install all together as one happy family. /bin /composer /doctrine /symfony autoload.php <-- Doctrine.php opens this to load its files 

then in your library file Doctrine.php (see below) you simply:

 require_once FCPATH . 'vendor/autoload.php'; // FCPATH is a CI constant specifies the path to the front controller. 

In addition, you can install all the files and folders contained within vendor in other places, for example, in third_party and configure Doctrine.php accordingly.


Integration with CodeIgniter

(The following instructions have been changed from: ORM Documentation Doctrine 2 - Integration with CodeIgniter )

  1. Create your Doctrine library: in your system/application/libraries folder, create a file called Doctrine.php and copy / paste the following code into the file. This will be your wrapper / bootstrap for the Doctrine2 entity manager.

    Your Doctrine.php library file should look like this (you can customize it 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'; // load Doctrine require_once FCPATH . 'vendor/autoload.php'; // or, if you installed another way, you could: // require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php'; // load the Doctrine classes $doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'libraries'); // or, if installed in third_party: // $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; endif; $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 (recommended to remove for production) $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); } } 
  2. Download the doctrine library: either automatically download the Doctrine library by adding it to the array in the application/config/autoload.php :

    '$ autoload [' library] = array ('doctrine);'

or load it manually into your controller, like any other library, using:

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

If you installed Doctrine.php in applications/third_party , you should use:

$autoload['libraries] = array('third_party/doctrine');

or

$this->load->library('third_party/doctrine');

An example of the controller is given below in the section What is next .


Command line tool setup

Doctrine comes with a number of command line tools that are very useful during development.

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

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

You need to register your EntityManager applications in the console 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 

Create display classes from the database:

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

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

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

For operating mode : Doctrine recommends changing the following parameters in Doctrine.php: - use a real caching system such as APC - disable EchoSqlLogger - disable autoGenerateProxyClasses


What's next

To use Doctrine in the CI, call it from the controller, for example:

application / controllers / my_controller.php:

 function doctrine_orm() { $this->load->library('Doctrine'); $em = $this->doctrine->em; // do Doctrine stuff $productRepository = $em->getRepository('Product'); $products = $productRepository->findAll(); foreach ($products as $product): echo sprintf("-%s\n", $product->getName()); endforeach; } 

However, before doing any things in Doctrine, you must first map the database tables to the Doctrine entities. Find out how here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/getting-started.html.

+6
source

For CI3 + HMVC + Doctrine 2.4

 <?php use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Cache\ArrayCache; use Doctrine\Common\ClassLoader; use Doctrine\Common\EventManager; use Doctrine\DBAL\Event\Listeners\MysqlSessionInit; use Doctrine\DBAL\Logging\EchoSQLLogger; use Doctrine\ORM\Configuration; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping\Driver\AnnotationDriver; use Doctrine\ORM\Tools\SchemaTool; use Gedmo\Sluggable\SluggableListener; use Gedmo\Timestampable\TimestampableListener; use Gedmo\Tree\TreeListener; class Doctrine { public $em = null; public $tool = null; public function __construct() { // Is the config file in the environment folder? if (!defined('ENVIRONMENT') OR !file_exists($file_path = APPPATH . 'config/' . ENVIRONMENT . '/database.php')) { $file_path = APPPATH . 'config/database.php'; } // load database configuration from CodeIgniter require $file_path; // Set up class loading. You could use different autoloaders, provided by your favorite framework, // if you want to. require_once APPPATH . 'vendor/doctrine/common/lib/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 . 'proxies'); $proxiesClassLoader->register(); foreach (glob(APPPATH . 'modules/*', GLOB_ONLYDIR) as $m) { $module = str_replace(APPPATH . 'modules/', '', $m); $loader = new ClassLoader($module, APPPATH . 'modules'); $loader->register(); } $evm = new EventManager; // timestampable $evm->addEventSubscriber(new TimestampableListener); // sluggable $evm->addEventSubscriber(new SluggableListener); // tree $evm->addEventSubscriber(new TreeListener); // 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 . '/proxies'); //must be set to 777 $config->setProxyNamespace('Proxies'); // Set up logger $logger = new EchoSQLLogger; $config->setSQLLogger($logger); if (ENVIRONMENT == "development") { $config->setAutoGenerateProxyClasses(true); } else { $config->setAutoGenerateProxyClasses(false); } // Database connection information $connectionOptions = array( 'driver' => 'pdo_mysql', 'user' => $db[$active_group]['username'], 'password' => $db[$active_group]['password'], 'host' => $db[$active_group]['hostname'], 'dbname' => $db[$active_group]['database'] ); // Create EntityManager $this->em = EntityManager::create($connectionOptions, $config); // Force UTF-8 $this->em->getEventManager()->addEventSubscriber(new MysqlSessionInit('utf8', 'utf8_unicode_ci')); // Schema Tool $this->tool = new SchemaTool($this->em); } } 
+1
source

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


All Articles