title: Using Doctrine to abstract CRUD operations
body: <p>This has bothered me for quite a while, but now it is necessity that I find the answer.</p>
  
  <p>We are working on quite a large project using CodeIgniter plus Doctrine.</p>
  
  <p>Our application has a front end and also an admin area for the company to check/change/delete data.</p>
  
  <p>When we designed the front end, we simply consumed most of the Doctrine code right in the controller:</p>
  
  <pre><code>//In semi-pseudocode
  function register()
  {
    $data = get_post_data();
  
    if (count($data) &amp;&amp; isValid($data))
    {
      $U = new User();
      $U-&gt;fromArray($data);
      $U-&gt;save();
  
      $C = new Customer();
      $C-&gt;fromArray($data);
      $C-&gt;user_id = $U-&gt;id;
      $C-&gt;save();
  
      redirect_to_next_step();
    }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>I have moved a lot of functionality (business logic) into the model using model methods, but the basic CRUD does not fit there.</p>
  
  <p>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).</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Your input is much appreciated.</p>