Codeigniter-Doctrine migration from Windows to Linux

Before I begin, Codeigniter is a php framework that implements the Model-View-Controller design architecture. Doctrine is an Object Relational Mapper and a powerful Database Abstraction Layer built on top of php. Doctrine handles all, if not most, database commands such as create, read, update and delete.

I have first started working with Doctrine and Kohana and did not come across any problems when I migrated from Windows to a Linux host. The problem came when I started working with Codeigniter and Doctrine.

Codeigniter and Doctrine are great frameworks and when combined you can create a powerful app but the problem comes when you make your application live. I’m sure that there are a few people who experience migration problems when moving from a Windows host to Linux using Codeigniter.

I got the idea of installing a Doctrine plugin to the Codeigniter framework from here.

It was all working fine locally as the development phase was on-going until the day came when we had to make the application live. A lot of problems started popping up like ‘case-sensitive filenames’, ‘file not found’, ‘invalid urls’ etc. It took us quite a while before the system was up and running fine.

We started fixing the problems one after the other by changing file name cases and applying url fixes. It was going well until we stumbled across the BaseDoctrineModel classes weren’t being found. BaseDoctrineModels are files automatically generated by the Doctrine framework. Examples of BaseDoctrineModels are ‘BaseTblUsers’, ‘BaseTblCars’, etc. These files are usually located in the ‘application/models/doctrine/generated/’ folder.

This is an example of the problem:

Fatal error: Class ‘BaseServiceCatagory’ not found

I was sure that it was working fine locally so then I tried searching the web for a solution, we weren’t able to find any solution and we were at our wits’ end. I tried out several things that to no avail didn’t work until I started manually including the BaseDoctrineModel classes. There’s a trick to manually including those files. Put them in the wrong location and you’ll end up with more errors.

This is how I fixed it:

Original Code:
[cc lang=”php” tab_size=”2″ lines=”-1″]
$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
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));
[/cc]

Add the fix:
[cc lang=”php” tab_size=”2″ lines=”-1″ first_line=”27″]
// CodeIgniter’s Model class needs to be loaded
require_once BASEPATH.’/libraries/Model.php’;

// fix
require_once APPPATH.’/models/doctrine/generated/BaseTblUsers.php’;
require_once APPPATH.’/models/doctrine/generated/BaseTblCars.php’
// /fix

// telling Doctrine where our models are located
Doctrine::loadModels(APPPATH.’/models’);
[/cc]

as seen in the fix, I manually included the BaseDoctrineModels and it seems to fix the problem. But I was sure it was working locally without this fix! It must be one of the wonders that stay in the gap between Windows and Linux.

Just to let you know that this is not the best solution. I’m guessing that there are more ways on how to fix this problem more efficiently and that it will depend on Codeigniter’s autoload functionality.

If you think or come up with a better solution please tell me.

I also posted this problem in StackOverflow: link.


Comments

2 responses to “Codeigniter-Doctrine migration from Windows to Linux”

  1. I haven’t checked Doctrine out yet but as was said above, it handles most CRUD operations. Django ORM has it so that database metadata is specified in the model, and then after running a code to sync the db, it then generates the necessary tables, which can then be manipulated with methods Django native methods. This also paves the way to an auto-generation of a backend CMS. Is this available in Doctrine?

    1. There are ways of how doctrine auto-generates either table in database or models. It can be seen here.

      In other words, models can be generated from an existing database or a given yml file.
      or tables in the database and models can be generated from a given yml file.
      or yml files and models can be generated from existing database.

      by auto-generation of back-end cms do you mean scaffolding? if so, doctrine doesn’t have this feature, it only auto-generates models and / or tables.

Leave a Reply to Ygam Retuta Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.