Misty-Stix

The Clouded Vision

Blog Hacking Misty Twits Portfolio About Contact

doctrine in kohana

programming

I've been using kohana and its query builder for a while now - its good but in time I came to notice that I begin rewriting code over and over again so I tried to break out of the kohana-database shell and tried out existing ORM frameworks. If you're curious about ORM check here - http://en.wikipedia.org/wiki/Object-relational_mapping. In short; using orm frameworks may greatly reduce the data-access layer code you write in your application and ensures functionality and ease in retrieving data and treating them as objects.

Without further ado; the integration of kohana and doctrine.

Instructions:
1) Download doctrine module here - http://dl.dropbox.com/u/617821/misty/doctrine.1.2.zip
2) Extract the doctrine folder to your modules directory. eg /public_html/modules/
3) Include doctrine in your modules list located in application/config/config.php
   

$config['modules'] = array
(
MODPATH.'doctrine',        // Doctrine
  // MODPATH.'archive',    // Archive utility
  // MODPATH.'payment',   // Online payments
  // MODPATH.'unit_test',    // Unit testing
);

4) Make sure you enable hooks for your particular application in application/config/config.php
   
$config['enable_hooks'] = TRUE;

5) Before running your application, create these configuration folders (be sure to set permissions to 777), doctrine will import and export doctrine-specific configurations here:
   
application/config/doctrine/sql
application/config/doctrine/fixtures
application/config/doctrine/migrations
application/config/doctrine/schema

6) Before running your application, create these models folders (be sure to set permissions to 777), doctrine will store and load your auto-generated models from here:
   
application/models/doctrine
application/models/doctrine/generated

7) Test your application. You can generate models from your database schema by executing this code:
   
Doctrine::generateModelsFromDb(
  'application/models/doctrine', 
  array('doctrine'), 
  array('generateTableClasses' => true)
);

   
And that's pretty much it to make doctrine work with kohana. I've tested this configuration both local and online. This configuration uses the application/config/database.php so you don't have to re-specify the credentials of your database elsewhere in the application.

If it doesn't work on your end don't hesitate to let me know. =)

Other Information:
    Tested working in: Kohana v2.3.4, PHP v5.2.6, MySQL v5.0.67-community-nt
    ORM framework used: Doctrine v1.2
   
Other References:
    http://www.mapledesign.co.uk/code/kohana-doctrine/ - I got most of the idea from here
    http://www.doctrine-project.org/documentation/manual/1_2/en/introduction - doctrine documentation / manual
    http://www.kohanaphp.com/ - kohana php framework

 

(3) Comments

christian reyes

christian reyes said...

hi olivier! glad to know that it works, well i never tried the validation of doctrine yet, i mostly depend on kohana's validation library at the moment.

Wednesday 6:10 pm, January 27, 2010

Olivier Moratin

Olivier Moratin said...

My mistake. I was not editing the hook but the command line stuff

Wednesday 7:15 am, January 27, 2010

Olivier Moratin

Olivier Moratin said...

Works beautifully so a big thank you! However, even though I added $manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL); the validation doesn't work.

Wednesday 7:07 am, January 27, 2010


Leave a comment

(required)
(optional)
(optional)
(required)
Captcha
(required)