3
votes

I have been trying out CodeIgniter. So far, I like it. I decided to also try out Doctrine because I hear so much about ORM and that Doctrine is great. I found some tutorials:

I have tried all of the methods that I found at those to try to set up a working Doctrine install, but I still can't get my model loaded. The one common thread in all of these docs is that they're old and reference old versions of Doctrine and/or CI, even Doctrine's own docs reference version 0.11. I am using CodeIgniter-1.7.2 and Doctrine-1.2.3.

Does anybody have up-to-date documentation? Can anybody see where the code below is going wrong? This is based off of the phpandstuff link.

File Structure
root
   \_system
        \_application
            \_config
            \_controllers
            \_models
            \_views
        \_plugins
            \_doctrine
                \_lib
                    \_Doctrine.php
            \_doctrine_pi.php

system/plugins/doctrine_pi.php

// load Doctrine library
require_once BASEPATH.'/plugins/doctrine/lib/Doctrine.php';

// load database configuration from CodeIgniter
require_once APPPATH.'/config/database.php';

// this will allow Doctrine to load Model classes automatically

//below is the suggested method of loading models
//I also tried a user's suggestion of "modelsAutoload"
//spl_autoload_register(array('Doctrine', 'autoload'));
//spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));

// we load our database connections into Doctrine_Manager
// this loop allows us to use multiple connections later on
foreach ($db as $connection_name => $db_values) {

    // first we must convert to dsn format
    $dsn = $db[$connection_name]['dbdriver'] .
        '://' . $db[$connection_name]['username'] .
        ':' . $db[$connection_name]['password'].
        '@' . $db[$connection_name]['hostname'] .
        '/' . $db[$connection_name]['database'];

    Doctrine_Manager::connection($dsn,$connection_name);
}

// CodeIgniter's Model class needs to be loaded
require_once BASEPATH.'/libraries/Model.php';

// telling Doctrine where our models are located
//Below is another user's suggestion, this also fails
//within my setup.
spl_autoload_register(array('Doctrine', 'autoload'));
spl_autoload_register(array('Doctrine', 'modelsAutoload'));
$manager = Doctrine_Manager::getInstance();

$manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);
$manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);
Doctrine::loadModels(APPPATH.'models');


// (OPTIONAL) CONFIGURATION BELOW

// this will allow us to use "mutators"
Doctrine_Manager::getInstance()->setAttribute(
    Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);

// this sets all table columns to notnull and unsigned (for ints) by default
Doctrine_Manager::getInstance()->setAttribute(
    Doctrine::ATTR_DEFAULT_COLUMN_OPTIONS,
    array('notnull' => true, 'unsigned' => true));

// set the default primary key to be named 'id', integer, 4 bytes
Doctrine_Manager::getInstance()->setAttribute(
    Doctrine::ATTR_DEFAULT_IDENTIFIER_OPTIONS,
    array('name' => 'id', 'type' => 'integer', 'length' => 4));

from system/application/config/autoload.php

/*
| -------------------------------------------------------------------
|  Auto-load Plugins
| -------------------------------------------------------------------
| Prototype:
|
|   $autoload['plugin'] = array('captcha', 'js_calendar');
*/

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

root/system/models/User.php

class User extends Doctrine_Record {

    function __construct(){
        parent::__construct();
    }

    function setTableDefinition(){
        $this->hasDefinition("username", "string", 255);
        $this->hasDefinition("password", "string", 255);
    }

}

If anyone wants to see my controller or anything else, let me know. The controller just echos "added user" and uses doctrine to save a user. This is met with a "Fatal error: Class 'User' not found..."

2

2 Answers

1
votes

I followed the tutorial here :: http://www.scribd.com/Ci-Doctrine/d/43303007

Note if you are in windows, the APPPATH may be not be set up nicely, eg you may get something like C:\wamp\www\v1/system/application/. So set your $system_folder to an absolute path in index.php on the root directory.

Enjoy

0
votes

This is how my setup looks like. It's for a Zend Framework app, but you should be able to translate it to CI with no problem.

try {
    Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_AGGRESSIVE);
    Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
    $conn = Doctrine_Manager::connection($this->_db->getConnection());
    Doctrine::loadModels($this->_root . '/application/models');
} catch (Zend_Db_Adapter_Exception $e) {
    echo 'Incorrect database login: ' . $e->getMessage();
}

I would also check that all your references to where the models are loaded are correct. This kind of problem is usually caused by a bad reference to where your doctrine library is or where your models are.