📜 ⬆️ ⬇️

Proper integration of Doctrine into CodeIgniter

Hello, the other day I started integrating the popular PHP-ORM Doctrine with the equally popular PHP framework CodeIgniter and found that the official way of integration voiced in the Doctrine kookbook and on the CodeIgniter wiki causes at least resentment to me.

Why? Because the instructions suggest:
  1. Change the contents of the file index.php;
  2. Use the configuration file application / config / database.php to initialize the connection and connect the CI.

What's bad about it? The fact that when updating the framework, we will have to go into the index.php file and make changes from time to time. The fact that in the configuration files should not be performed system actions. And also the fact that in the end we connect the database.php configuration file via index.php, completely ignoring the flexible framework tools.

I propose to the habrayers their way of connecting Doctrine to CodeIgniter, which is devoid of the above disadvantages. So, let's begin:
  1. First of all, download the latest version of Doctrine from here ;
  2. Copy the contents of the lib directory from the downloaded archive into the system / database / doctrine / directory;
  3. I try to always think logically, therefore, to connect the ORM library, we use the library mechanism in CI. In the application / libraries directory we place the doctrineORM.php file with the following code:

    <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    1. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    2. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    3. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    4. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    5. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    6. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    7. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    8. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    9. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    10. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    11. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    12. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    13. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    14. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    15. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    16. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    17. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    18. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    19. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    20. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    21. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    22. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    23. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    24. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    25. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    26. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    27. <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .
    <?php if (!defined( 'BASEPATH' )) exit( 'No direct script access allowed' ); /** * Doctrine initialization class */ class DoctrineORM { function __construct() { // require_once(APPPATH . 'config/database.php' ); // DSN $db[ 'default' ][ 'dsn' ] = $db[ 'default' ][ 'dbdriver' ] . '://' . $db[ 'default' ][ 'username' ] . ':' . $db[ 'default' ][ 'password' ]. '@' . $db[ 'default' ][ 'hostname' ] . '/' . $db[ 'default' ][ 'database' ]; // Doctrine.php require_once(BASEPATH . 'database/doctrine/Doctrine.php' ); // autoloader spl_autoload_register(array( 'Doctrine' , 'autoload' )); // Doctrine_Manager::connection($db[ 'default' ][ 'dsn' ], $db[ 'default' ][ 'database' ]); // "conservative/lazy" Doctrine_Manager::getInstance()->setAttribute( 'model_loading' , 'conservative' ); // autoloader Doctrine::loadModels(APPPATH . 'models' ); } } * This source code was highlighted with Source Code Highlighter .

    This file will be responsible for the initialization of Doctrine within our project. It turns out a kind of loader;
  4. At the root of our application we place the files doctrine and doctrine.php
    ')
    doctrine:
    1. #! php
    2. <? php
    3. // System and application directories relative to the current
    4. $ sys_folder = '../' ;
    5. $ app_folder = '.' ;
    6. include ( 'doctrine.php' );
    * This source code was highlighted with Source Code Highlighter .


    doctrine.php:
    1. <? php
    2. define ( 'BASEPATH' , str_replace ( '\\' , '/' , $ sys_folder). '/' );
    3. define ( 'APPPATH' , str_replace ( '\\' , '/' , $ app_folder). '/' );
    4. require_once (APPPATH. 'libraries / doctrineORM.php' );
    5. new DoctrineORM ();
    6. // Configure "Doctrine Cli"
    7. $ config = array (
    8. 'data_fixtures_path' => APPPATH. '/ fixtures' ,
    9. 'models_path' => APPPATH. '/ models' ,
    10. 'migrations_path' => APPPATH. '/ migrations' ,
    11. 'sql_path' => APPPATH. '/ sql' ,
    12. 'yaml_schema_path' => APPPATH. '/ schema'
    13. );
    14. $ cli = new Doctrine_Cli ($ config);
    15. $ cli-> run ($ _ SERVER [ 'argv' ]);
    * This source code was highlighted with Source Code Highlighter .

    These files will be responsible for the Doctrine CLI interface. We will be able to communicate with Doctrine through the console instruction "php doctrine", executed from the application directory;
  5. Create subdirectories in the application directory:
    • application / fixtures;
    • application / migrations;
    • application / schema;
    • application / sql.

  6. Enter the correct data for connecting to the DBMS in application / config / database.php. This should be done just as if you were working with a native ORM CodeIgniter;
  7. Add the doctrineORM library to the list of loadable libraries in the application / config / autoloader.php configuration file

    Example:
    1. $ autoload [ 'libraries' ] = array ( 'doctrineORM' , 'session' );
    * This source code was highlighted with Source Code Highlighter .


Everything. Now we can safely use Doctrine inside the controllers and views related to this application.
Let's try a simple application example using our newcomer ORM =)
  1. Create a user.yml file in the application / schema directory with the following structure:

    1. ---
    2. User:
    3. columns:
    4. id:
    5. primary: true
    6. autoincrement: true
    7. type: integer (4)
    8. username: string (255)
    9. password: string (255)
    10. relations:
    11. Groups: # Relation alias or class name
    12. class : Group # Class name. Optional if alias is the class name
    13. local: user_id # Local: User.id = UserGroup.user_id. Optional
    14. foreign: group_id # Foreign: Group.id = UserGroup.group_id. Optional
    15. refClass: UserGroup # xRefClass for Users
    16. foreignAlias: Users # Opposite relationship alias. Group hasMany Users
    17. Group:
    18. tableName: groups
    19. columns:
    20. id:
    21. primary: true
    22. autoincrement: true
    23. type: integer (4)
    24. name: string (255)
    25. UserGroup:
    26. columns:
    27. user_id:
    28. type: integer (4)
    29. primary: true
    30. group_id:
    31. type: integer (4)
    32. primary: true
    33. relations:
    34. User:
    35. local: user_id # Local key
    36. foreign: id # Foreign key
    37. onDelete: CASCADE # Database constraint
    38. Group:
    39. local: group_id
    40. foreign: id
    41. onDelete: CASCADE
    * This source code was highlighted with Source Code Highlighter .

  2. Open the console (or command line) and go to the directory of our application (application);
  3. Running the following command will create models in the application / models directory based on the user.yml structure.
    $ php doctrine generate-models-yaml<br>generate-models-yaml - Generated models successfully from YAML schema <br><br> * This source code was highlighted with Source Code Highlighter .

  4. Now you can create some fictitious data to bring them into the DBMS. To do this, create the file application / fixtures / users.yml
    1. ---
    2. User:
    3. jwage:
    4. username: jwage
    5. password: test
    * This source code was highlighted with Source Code Highlighter .

  5. Now run the build-all-reload task to clean up the database, create models and re-create the structure in the database
    $ php doctrine build-all-reload<br>build-all-reload - Are you sure you wish to drop your databases? (y/n)<br>y <br><br> * This source code was highlighted with Source Code Highlighter .

  6. Now you can use Doctrine to get / write data directly from our controller. In the welcome controller, the index () method add the following code and open
    project home page in browser
    1. $ user = new User ();
    2. $ user-> username = 'zYne-' ;
    3. $ user-> setPassword ( 'password' );
    4. $ user-> save ();
    5. $ userTable = Doctrine :: getTable ( 'User' );
    6. $ user = $ userTable-> findOneByUsername ( 'zYne-' );
    7. echo $ user-> username; // prints 'zYne-'
    * This source code was highlighted with Source Code Highlighter .


I hope someone helped. I apologize that in a personal blog, and not in CodeIgniter, there is not enough karma.
UPD: moved to "CodeIgniter"
UPD2: in config / autoload.php you must turn off the “database” from the list of loaded libraries

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


All Articles