Service Layer in Codeigniter 2 with Doctrine 2 Service Layer in Codeigniter 2 with Doctrine 2 codeigniter codeigniter

Service Layer in Codeigniter 2 with Doctrine 2


I found a solution for my problem, hope it works for some of you.

I'm using Joel's Verhagen integration of Codeigniter 2 and Doctrine 2, you can read his article for more detail "http://www.joelverhagen.com/blog/2011/05/setting-up-codeigniter-2-with-doctrine-2-the-right-way/"

In simple words what I'm doing is using Codeigniter's Models as a Service layer.This was the cleanest approach that i could find, mainly because all the "wiring" is already done by Codeigniter, so I didn't have to make anything else :D.

I had to make some modifications to the folder structure of Joel's implementation, that way i can use CI's models and still using his Doctrine code. So I moved everything from inside the folder "models" to a new folder called "entities" (I know it might not be the best name, but it works :P).Then I changed all the references to the new folder and checked that everything worked.

That's it, now i have my "service layer" workign and my code is a lot more cleaner.

If some of you need help with this, please feel free to ask me.


Was in the same boat a while back. Ended up not using Doctrine's ORM, but basically you're right - you need a "service layer" for anything that isn't directly modeled via Doctrine's entities and repositories.

The way I do this creating a namespaced folder in /application/ for my project code. I then used Doctrine Common's class loader to recognize that folder as the namespace. For example /application/Acme/Authentication.php contains:

namespace Acme;class Authentication {   //Do Doctrine queries in various methods here}

Doctrine's class loader uses SPL (spl_autoload_register or something) internally. What that means is that you can fully use PHP 5.3 namespaces. You then have all the fun trials and tribulations of dependency injection for accessing the doctrine dbal inside this service layer. Your controllers will then use this "service layer" directly. Like I said, in my case I decided not to use Doctrine's ORM - so I'm using CodeIgniters ActiveRecord database classes inside my "service layer". Rather than using $this->CI =& get_instance() ... I'm providing the database access into the constructors using a DI container.

For example in my auth/login controller action I may have

function login() {   $user = $_POST['u'];   $pass = $_POST['p'];   $auth = new Acme\Authentication($this->db); //or use a DI container   $user = $auth->authenticate($user, $pass);   ....}