📜 ⬆️ ⬇️

Screw Doctrine 2 ORM to Silex

Recently I found out about the magnificent micro-framework Silex , and about the same recently about the model ORM . Tutorial will be useful for those who want to connect one with the other.
Silex is a lightweight, extensible micro-framework built on Symfony 2 and Pimple components .
Doctrine is an ORM engine that allows you to work with a database not directly, but through ordinary objects.
The article was written based on this , but is not its translation.
Well, let's try to connect one with the other. To install the libraries will use Composer .

Define dependencies in composer.json


 { "minimum-stability": "dev", "require": { "php": ">=5.3.3", "silex/silex": "1.*", "taluu/doctrine-orm-provider": "1.0.*" }, "config": { "bin-dir": "bin" } } 

It is necessary to explain the config field. In it, we specify the place where Composer will create shortcuts (aliases) for files executed in the console.
Run composer install

Add the application code


At the root of the project, create an application folder - app/src , or any other to choose from. Following the pattern (which, by the way, can be established, which I did not do), I called src .
Add the following code to index.php :
 // index.php <?php $app = require __DIR__.'/src/app.php'; $app->run(); 

Here we unload the application in $ app and launch it.
What is inside app.php:
 // src/app.php <?php require __DIR__.'/../vendor/autoload.php'; $app = new Silex\Application(); $app['debug'] = true; require __DIR__.'/registers.php'; require __DIR__.'/controllers.php'; return $app; 

And here we connect the class autoloader created by Composer, create the application and configure it.
In controllers.php we describe the logic of the controllers: $ app-> get (), $ app-> error (), and so on.
We are interested in registers.php:
 // src/registers.php <?php use Doctrine\Common\Cache\ApcCache; use Doctrine\Common\Cache\ArrayCache; $app->register(new Silex\Provider\DoctrineServiceProvider(), array( 'db.options' => array(//   DBAL : http://silex.sensiolabs.org/doc/providers/doctrine.html 'driver' => 'pdo_mysql', 'dbname' => 'silex_test', 'host' => '127.0.0.1', 'user' => 'root', 'password' => 'root', 'charset' => 'utf8' ) )); $app->register(new Nutwerk\Provider\DoctrineORMServiceProvider(), array( 'db.orm.proxies_dir' => __DIR__.'/../cache/doctrine/proxy', 'db.orm.proxies_namespace' => 'DoctrineProxy', 'db.orm.cache' => !$app['debug'] && extension_loaded('apc') ? new ApcCache() : new ArrayCache(), 'db.orm.auto_generate_proxies' => true, 'db.orm.entities' => array(array( 'type' => 'annotation', //     Entity 'path' => __DIR__, // ,    'namespace' => 'TestApp\Entity', //   )), )); 

According to the specified namespace, create directories, in this case:
src/TestApp and src/TestApp/Entity
')

Configure Doctrine Console Management



You'll have to change the code a bit in the console tools (just the console) Doctrine, available thanks to Composer's bin .
To connect the console and our application we cli-config.php configure cli-config.php . Initially, this file is in vendor/doctrine/orm/tools/sandbox , copy from there to bin . And now remove everything except the creation of $helpers .
All remote code created and configured what we configured for our application. So why don't we use it?
 // bin/cli-config.php <?php $app = require __DIR__.'/../src/app.php'; $em = $app['db.orm.em']; $helpers = 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) )); 

It is worth adding that working in our application with Doctine, we can get a copy of EntityManager from $app['db.orm.em']
Options for connecting these settings:

  1. Not the right option, so do not:
    Fix bin / doctrine.php:
      $configFile = getcwd() . DIRECTORY_SEPARATOR . 'bin/cli-config.php'; // 21  

    This could not be done by placing cli-config.php in the root of the project. On the one hand, it is not correct to edit the library code, on the other hand, it is convenient.
  2. You can go in another, more correct way - to create your own console and connect your configs yourself:
     // bin/console #!/usr/bin/env php <?php require 'cli-config.php'; include('doctrine.php'); 

    Now you need to give permissions to execute bin / console and to run from the project root use:
     ./bin/console [command_name] 

What else to read on:
Russian Doctrine Documentation
English Doctrine ORM documentation
UPD: Added on github a repository with a framework of the application on silex and doctrine orm. True, another provider is used for ORM.

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


All Articles