Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 2.0 with Doctrine 1.2.2 problems [SOLVED]
#1

[eluser]adam28[/eluser]
Hi guys,

Two issues.

Firstly, I've been working on integrating the Doctrine 1.2.2 ORM (not quite ready to move the codebase to PHP 5.3 yet) with CodeIgniter 2.0 for the last hour or so, but a small problem is now doing my head in, and I can't work out what is going wrong.

I was roughly following the phpandstuff article (http://www.phpandstuff.com/articles/code...-and-setup) replacing reference to the plugins as a library.

I've hit a brick wall with the implementation of this though. In the doctrine.php file that is autoloaded in /application/libraries/ there is a require_once for the database config file in order to get the connection information obviously. Strangely, the $db array isn't accessible to the doctrine.php file that is including it.

If I go into the /system/core/Loader.php and add an include for the database config file *just* before the doctrine library is autoloaded, then I can use the $db array.

Any ideas why when I try to include it from the doctrine file itself, it fails?

This is the error below:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: db

Filename: libraries/doctrine.php

This is the code of my doctrine.php file (only slightly modified from the example while I'm trying to get it working)

Code:
<?php
// application/libraries/doctrine.php

// load Doctrine library
require_once APPPATH.'/libraries/Doctrine/lib/Doctrine.php';

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

// this will allow Doctrine to load Model classes automatically
spl_autoload_register(array('Doctrine', 'autoload'));

// 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.'/core/Model.php';

// telling Doctrine where our models are located
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));

Secondly, if for hackings sake, I duplicate the $db array inside the doctrine.php file the errors concerning undefined variables goes away, but I get left with the following fatal error:

----------------------------------------
Fatal error: Uncaught exception 'Doctrine_Exception' with message 'Doctrine is static class. No instances can be created.' in /var/www/projects/fusion/application/libraries/Doctrine/lib/Doctrine/Core.php:510 Stack trace: #0 /var/www/projects/fusion/system/core/Loader.php(923): Doctrine_Core->__construct() #1 /var/www/projects/fusion/system/core/Loader.php(823): CI_Loader->_ci_init_class('doctrine', '', NULL, NULL) #2 /var/www/projects/fusion/system/core/Loader.php(100): CI_Loader->_ci_load_class('doctrine', NULL, NULL) #3 /var/www/projects/fusion/system/core/Loader.php(987): CI_Loader->library('doctrine') #4 /var/www/projects/fusion/system/core/Controller.php(57): CI_Loader->_ci_autoloader() #5 /var/www/projects/fusion/system/core/CodeIgniter.php(281): Controller->Controller() #6 /var/www/projects/fusion/index.php(163): require_once('/var/www/projec...') #7 {main} thrown in /var/www/projects/fusion/application/libraries/Doctrine/lib/Doctrine/Core.php on line 510
----------------------------------------

Any one run into this issue before and have a solution?

Cheers!
Adam.
#2

[eluser]adam28[/eluser]
Found the second issue, CI is trying to create an instance of the class in /system/core/Loader.php

Someone suggested making it into helper instead. So I might try that.
#3

[eluser]adam28[/eluser]
Making it a helper fixed both issues Smile

I'll give it a test run and see how well it works now.
#4

[eluser]adam28[/eluser]
Runs perfectly Smile
#5

[eluser]fobin[/eluser]
Anyway you could post details of how you got it up and running? I'm quite new to Doctrine and I'm strucling to get it running on CI 2.0.
#6

[eluser]mattalexx[/eluser]
[quote author="adam28" date="1280146953"]Runs perfectly Smile[/quote]

Any chance you might share your solution with the world?
#7

[eluser]Unknown[/eluser]
Just in case anyone is still stuck with this here's what you need to do:

Move the doctrine folder and your doctrine php file out of libraries (or plugins if it's still in there) and into the helpers folder.
Rename your doctrine php file doctrine_helper.php
In this file you need to amend the first require_once to use the correct new path, probably APPPATH.'/helpers/doctrine/lib/Doctrine.php';

I also have doctrine autoloaded so you need to make sure that you update your config/autoload.php file to load doctrine as a helper, not a library or plugin.

If you have also just upgraded to codeigniter 2 then make sure in your doctrine_helper.php file that you're loading codeigniter's model class from the core folder not the libraries folder, so line 28 becomes require_once BASEPATH.'/core/Model.php';

This should make it all work fine!
#8

[eluser]portgaz[/eluser]
Thanks Smile
It solved my problem integrating codeigniter2 and doctrine2..
#9

[eluser]Unknown[/eluser]
Hi

Had the same issue on Codeigniter 2 + Doctrine 2, I was autoloading the database library in the autoload.php, removing it from autoload solved the problem. BTW, does anyone know why does it happen?




Theme © iAndrew 2016 - Forum software by © MyBB