March 2011

Integrate Doctrine 1.x to Codeigniter 2.0

Submitted by audy on Sun, 2011-03-06 16:03
  • Download Doctrine 1.2.3 from http://www.doctrine-project.org/downloads/Doctrine-1.2.3.tgz

  • Extract Doctrine then put Doctrine.php and Doctrine folder to directory "system/database"

  • Create following folders under "application" folder

    - fixtures/data
    - migrations
    - schema
    - sql

  • Add this code into "application/config/database.php"

    /**
    * Doctrine integration
    */
    $db['default']['cachedir'] = "";
    // Create dsn from the info above
    $db['default']['dsn'] = $db['default']['dbdriver'] .
                            '://' . $db['default']['username'] .
                            ':' . $db['default']['password'].
                            '@' . $db['default']['hostname'] .
                            '/' . $db['default']['database'].
                            '' . '?charset=utf-8';
     
    // Require Doctrine.php
    require_once(realpath(dirname(__FILE__) . '/../..') . DIRECTORY_SEPARATOR . 'system/database/Doctrine.php');
     
    // Set the autoloader
    spl_autoload_register(array('Doctrine', 'autoload'));
     
    // Load the Doctrine connection
    Doctrine_Manager::connection($db['default']['dsn'], $db['default']['database']);
     
    // Load the models for the autoloader
    Doctrine::loadModels(realpath(dirname(__FILE__) . '/..') . DIRECTORY_SEPARATOR . 'models');

  • Add doctrine.php to "application" folder with this code

    <?php
    require_once('config/database.php');
     
    // Configure Doctrine Cli
    // Normally these are arguments to the cli tasks but if they are set here the arguments will be auto-filled
    $config = array('data_fixtures_path'  =>  dirname(<strong>FILE</strong>) . DIRECTORY_SEPARATOR . '/fixtures',
                    'models_path'         =>  dirname( <strong>FILE</strong> ) . DIRECTORY_SEPARATOR . '/models',
                    'migrations_path'     =>  dirname( <strong>FILE</strong> ) . DIRECTORY_SEPARATOR . '/migrations',
                    'sql_path'            =>  dirname( <strong>FILE</strong> ) . DIRECTORY_SEPARATOR . '/sql',
                    'yaml_schema_path'    =>  dirname( <strong>FILE</strong> ) . DIRECTORY_SEPARATOR . '/schema',
                    'generate_models_options' => array(
                            'generateBaseClasses' => true,
                            'baseClassPrefix' => 'Base_',
                            'baseClassesDirectory' => 'Base',
                            'classPrefixFiles' => false,
                            'classPrefix' => 'Model_',
                            'phpDocName' => '',
                            'phpDocEmail' => ''
                        ));
     
    $cli = new Doctrine_Cli($config);
    $cli->run($_SERVER['argv']);

  • Add "require_once APPPATH.'config/database.php';" into "index.php" before "require_once BASEPATH.'core/CodeIgniter'.EXT;"

  • Open command line window then go to "application" for run Doctrine CLI

Run CLI first time

You will see error message "No direct script access allowed"
For tempolary fix this problem, open "application/config/database.php" then add PHP Comment to first line

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

run CLI again
Run CLI success

  • Create "schema.yml" into "application/schema" for create sample model.

    Users:
      columns:
        name: string
        email: string

  • Create Doctrine model by run command "php doctrine.php build-all"
    Run CLI success
    If you check your database you will see new table in your database.
    New table was create

table structure

  • You can test new Doctrine model by open "application/welcome.php" and modified 'index' action to look like this
    function index()
         {
                //$this->load->view('welcome_message');
                $user = new Model_Users();
     
                // assign value
                $user->name = 'John Doe';
                $user->email = 'john@someemail.com';
     
                // save new User
                if($user->trySave()) {
                    echo 'User ID = '.$user->id;
                }
         }

    and then refresh your web site.
    Result in browser
    and this is sample data in database.
    Data in table

Done!

Drupal theme by Kiwi Themes.