Using Doctrine for Abstract CRUD Operations

This bothered me for quite some time, but now I need to find the answer.

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

Our application has an interface, as well as an administration area for checking / changing / deleting data.

When we designed the front end, we simply consumed most of the Doctrine code directly 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 start duplicating the admin code, and we decided that we were in “get it DONE” mode, so it now stinks with the bloat code.

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

CRUD , Customer:: save ($ array) [ , , prikey ], Customer:: delete ($ id), Customer:: getObj ($ id = false) [ false, ]. , 32 ( ).

, ( ), .

( -), , , - 3 "", CRUD - (), - , ?

.

+3
2

?

class RegistrationManager {
 public function register( $postData, $callBack ){
      $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();
        $callBack(); //I like this but you need PHP5.3
      }
     }
 }

:

$r = new RegistrationManager;
$r->register( get_post_data(), function(){ redirect_to_next_step(); } );

( ), , .

+2

, class'es

class UserTable extends Doctrine_Table
{
  public function register()
  {
    // There you do data model (not concrete object) related stuff
  }
}

http://www.doctrine-project.org/projects/orm/1.2/docs/cookbook/code-igniter-and-doctrine/en

+2

Source: https://habr.com/ru/post/1748263/


All Articles