Hy guys,
I’ve been using this really cool ORM library called Outlet and I’d like to share how I managed to integrate it on CI. Since it is my first contribution to the community, just let me know if I’m doing anything wrong or forgetting something here.
Since english is not my native language, sorry about any incorrect use of words or mispelling.
—————————————————————————————————————
Here is the site and documentation. For this post, I’m going to assume that you’ve created your models and config file needed to get the libray working.
This is the folder structure I built:
../application/config
- outlet-config.php
../application/models
- models_include.php // file to include models
- ... models ...
../application/outlet
- ... library files ...
../application/libraries
- MY_Loader.php // Extends CI loader and adds a new method to load Outlet
To load the library, I extended CI loader and added this method
class MY_Loader
function outlet()
{
static $loaded = false;
if ($loaded) return;
// Includes models
include_once APPPATH.'/models/models_include.php';
// Sets outlet include path
ini_set('include_path', ini_get('include_path').';'.realpath(APPPATH.'/outlet'));
require_once 'Outlet.php';
Outlet::init(include APPPATH.'/config/outlet-config.php');
$outlet = Outlet::getInstance();
$outlet->createProxies();
$loaded = true;
}
}
And to avoid name conflicts, I did a small change to the /system/codeigniter/CodeIgniter.php so that all controllers will need the ‘Controller’ suffix
....
/*
* ------------------------------------------------------
* Security check
* ------------------------------------------------------
* ...
*/
$class = $RTR->fetch_class().'Controller'; // Here is the change
$method = $RTR->fetch_method();
if ( ! class_exists($class)
OR $method == 'controller'
...
I did that so that I can have a Order entity, an OrderController and still use a clean URL /index.php/order without any change to the CI core
I’m actually using the repository pattern but to make things simple, here’s a sample usage:
class OrderController extends Controller
{
function show($id)
{
$this->load->outlet();
$outlet = Outlet::getInstance();
$order = $outlet->load('Order', $id);
// display order data...
}
}
The ‘models_include.php’ file is really simple:
/**
* Include all models
*/
require_once 'Order.php';
require_once 'OrderLine.php';
// ... other entities ...
I hope you guys like it
—
Fábio Rehm
