Doctrine ORM 1.2.4 in Fuel PHP 1.1

Today, I decided to make an attempt to dive into a new framework and implement my favorite ORM framework, Doctrine. As you can see, I’ve been spoiled really hard by Doctrine and I’ve perhaps reached a point in my career where it could be best to use it to hasten the development process and of course, one of its key points of being a powerful ORM framework.

The first thing you need to do is make fuelPHP work on your box. Once ready, download Doctrine 1.2.4 here: http://www.doctrine-project.org/downloads/Doctrine-1.2.4.tgz

On a default fuelPHP installation, extract the doctrine source files into [www]/fuel/packages. You should see something similar:

Create the bootstrap file “bootstrap.php” and it should be placed in the [www]/fuel/packages/doctrine/ folder:

file: [www]/fuel/packages/doctrine/bootstrap.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
require_once(dirname(__FILE__) . '/Doctrine.php');

spl_autoload_register(array('Doctrine', 'autoload'));
spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));

$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE);
Doctrine_Core::loadModels(APPPATH.'classes/model');

Config::load("db",true);
$config = Config::get("db");

$dsn = $config["default"]["connection"]["dsn"];
$user = $config["default"]["connection"]["username"];
$password = $config["default"]["connection"]["password"];

$dbh = new PDO($dsn, $user, $password);

$conn = Doctrine_Manager::connection($dbh);

By looking at the bootstrap file, Doctrine uses the database configuration of fuelPHP so that you don’t have to specify the connection details again when loading Doctrine leaving the configuration in one place.

Modify your fuelPHP config file by adding the doctrine package.
file: [www]/fuel/app/config/config.php

187
188
189
190
'packages'  => array(
      'doctrine'
      //'orm',
    ),

And finally, test!
This is just a sample code that will let you know if Doctrine is connecting to your database properly. If configured correctly, you should be able to see the tables in your database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  function action_doctrine()
  {
    $path = Doctrine::getPath();
 
    $conn = Doctrine_Manager::connection();
    $result = $conn->execute('SHOW TABLES;')->fetchAll();
    $tables_found = null;
   
    foreach ($result as $table)
    {
      $tables_found .= $table[0]."<br />";
    }
 
    $disp = "doctrine loaded from: {$path}";
    $disp .= "<hr />parsing tables... tables found: ";
    $disp .= "<blockquote>{$tables_found}</blockquote>";
 
    return $disp;
  }

This should get you started with your Indoctrinated FuelPHP project!

Notes:
It still does not work on my box! What do I do???
– check your configurations. verify the username, password and dsn connection
– check the extract paths
– post a comment and I’ll try to check

Packages used:
– Doctrine ORM v1.2.4
– FuelPHP v1.1

Platform (tested):
– xampp v1.7.7 on windows 7 – 64-bit
– php v5.3.8
– mysql v5.5.16

Questions / Suggestions? Ask!

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 Enlightened Thoughts, Techy Thoughts and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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