Skip to content

Using Propel as Model

Derek Jones edited this page Jul 4, 2012 · 16 revisions

Category:Library::Database | Category:Library::External

This document has not been updated sometime so I will try to complete it from the experience I had with Propel. I have been using Propel 1.3 with CI and it has been working great!!

First, install Propel to your system. I recommend using Propel 1.3 as it uses PDO instead of Creole for DB abstraction and the performance gain is as much as 100% against Creole.

URL to the Propel-Framework: [url]http://propel.phpdb.org/trac/[/url]

Please follow the steps below to setup Propel in your system:

[url]http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3/Installation[/url] [url]http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3/QuickStart[/url]

Now you have Propel in your system so let's try to integrate it with CI!

[b]1. Setup include_path for Propel core and class files[/b] The directory where propel class files are generated needs to be included in include_path environment variable. Also directory for Propel core files can be included if you want to use relative path in the Propel library file for CI.

I have set the include_path in my apache config file so the setting looks like this:

 php_value include_path ".:/home/me/htdocs:/home/me/propel_classes:/usr/share/php"

This means that propel class files are found in "/home/me/propel_classes" and Propel include file "propel/Propel.php" is found under "/usr/share/php".

Of course you can also set include_path at runtime or in .htaccess.

[b]2. Create Propel library in CI[/b] In your system/application/library directory of CI, create a file called Propel.php with the following content:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once('propel/Propel.php');
Propel::init("conf/yourdb-conf.php");

?>

"yourdb-conf.php" should be altered to whichever database name you have decided to use.

[b]3. (optional) Include Propel in autoload.php[/b] If you don't want to include the Propel library for every Controller you will be creating you can include that in system/application/config/autoload.php

$autoload['libraries'] = array('propel');

[b]4. Include Propel class file in controller[/b] You are ready to use Propel now so let's try to include it in your controller.

For example, Home Controller in system/application/controller/home.php that loads Propel class "Message.php" looks like this:

<?php

class Home extends Controller {

  function __construct()
  {
    parent::Controller();

    require_once('Message.php');
  }

  function index() {
     //do whatever you need to do here 
  }
}

?>

If you get error from CI not being able to load "Message.php" try giving read permission to the file by running something like "chmod -R 755 /home/me/propel_classes".

Pretty simple, no? Enjoy!!

Clone this wiki locally