Using Doctrine to abstract CRUD operations

Posted by TomWilsonFL on Stack Overflow See other posts from Stack Overflow or by TomWilsonFL
Published on 2010-06-03T01:36:21Z Indexed on 2010/06/03 1:44 UTC
Read the original article Hit count: 314

Filed under:
|
|
|

This has bothered me for quite a while, but now it is necessity that I find the answer.

We are working on quite a large project using CodeIgniter plus Doctrine.

Our application has a front end and also an admin area for the company to check/change/delete data.

When we designed the front end, we simply consumed most of the Doctrine code right in the controller:

//In semi-pseudocode
function register()
{
  $data = get_post_data();

  if (count($data) && isValid($data))
  {
    $U = new User();
    $U->fromArray($data);
    $U->save();

    $C = new Customer();
    $C->fromArray($data);
    $C->user_id = $U->id;
    $C->save();

    redirect_to_next_step();
  }
}

Obviously when we went to do the admin views code duplication began and considering we were in a "get it DONE" mode so it now stinks with code bloat.

I have moved a lot of functionality (business logic) into the model using model methods, but the basic CRUD does not fit there.

I was going to attempt to place the CRUD into static methods, i.e. Customer::save($array) [would perform both insert and update depending on if prikey is present in array], Customer::delete($id), Customer::getObj($id = false) [if false, get all data]. This is going to become painful though for 32 model objects (and growing).

Also, at times models need to interact (as the interaction above between user data and customer data), which can't be done in a static method without breaking encapsulation.

I envision adding another layer to this (exposing web services), so knowing there are going to be 3 "controllers" at some point I need to encapsulate this CRUD somewhere (obviously), but are static methods the way to go, or is there another road?

Your input is much appreciated.

© Stack Overflow or respective owner

Related posts about php

Related posts about design