Guest User

Untitled

a guest
Aug 26th, 2012
57
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  
  4. // UnitOfWork + DataMapper pattern
  5.  
  6. // Ideally clear DTO
  7. class Person
  8. {
  9.     public $name;
  10.     public $age;
  11. }
  12.  
  13. // plus a mapper / or a config (xml / yml / php ) file
  14. class PersonMapper extends DataMap
  15. {
  16.     // overriden properties, specifying how to map this item
  17.     private $tableName = 'users';
  18.     private $primaryKey = 'name';
  19. }
  20.  
  21. class UnitOfWork
  22. {
  23.     private $databaseConnection;
  24.     public function Insert( object ) { getMappingDataFromPersonMapper  }
  25.     public function Update( object )
  26.     public function Delete( object )
  27.     public function LoadById( int $id )
  28.     public function LoadByParams( array $arr )
  29. }
  30.  
  31. //Usage:
  32. $Person = $UnitOfWork->LoadById(1);
  33. $Person->age++;
  34. $UnitOfWork->update($Person);
  35.  
  36.  
  37. //-------------------------------------------------------//
  38. //-------------------------------------------------------//
  39. //-------------------------------------------------------//
  40.  
  41.  
  42. // Active record pattern
  43. class ActivePerson extends ActiveRecordBase
  44. {
  45.     public $name;
  46.     public $age;
  47.  
  48.     // inherited members / objects
  49.     private $tableName = 'users';
  50.     private $primaryKey = 'name';
  51.  
  52.     public function save()
  53.     public static function findByParams( arary $x = ['name' => 'Alex'] )
  54.     public static function loadById( int $id )
  55.     public function delete()
  56. }
  57.  
  58.  
  59. //usage
  60. $Person = ActivePerson::loadById(1);
  61. $Person->age++;
  62. $Person->save();
RAW Paste Data