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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
// system/application/plugins/doctrine_pi.php

// load Doctrine library
require_once APPPATH.'/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
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.'/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));

Add the fix:

27
28
29
30
31
32
33
34
35
36
// 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');

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.

Let the others know about this Rant
  • Print
  • Facebook
  • Twitter
  • LinkedIn
  • Plurk

About Christian Noel

Hi, I'm Cris! I'm interested in anime, programming and photography. My profession is programming and am mostly focused in web development. I've been programming since College. What inspired me to go to programming was because I was so into the gaming industry and I wanted to create my own game.
This entry was posted in Techy Thoughts and tagged , , , , , , . Bookmark the permalink.

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?

    • 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

Your email address will not be published. Required fields are marked *