Guest User

Untitled

a guest
Jan 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. class Admin extends User
  2. {
  3. // Other methods
  4.  
  5. public function save()
  6. {
  7. parent::save();
  8. // now save your additional fields wherever they need to go
  9. }
  10. }
  11.  
  12. $entity = new Admin;
  13. $mapper = new AdminMapper( $db );
  14.  
  15. $entity->setName('Wintermute');
  16. $mapper->fetch( $entity );
  17. //retrieve data for admin with that name
  18.  
  19. $entity->setName('Neuromancer');
  20. $mapper->store( $entity );
  21. // rename and save
  22.  
  23. class User
  24. {
  25. public function Save()
  26. {
  27. $savestuff = $this->GetProperties();
  28. // do save acctions
  29. }
  30.  
  31. protected function GetProperties()
  32. {
  33. return 'user prop';
  34. }
  35. }
  36.  
  37. class Admin Extends User
  38. {
  39. protected function GetProperties()
  40. {
  41. return 'admin prop';
  42. }
  43. }
  44.  
  45. class Admin
  46. {
  47. public function save(Model_Abstract $model = null)//type hinting
  48. {
  49. if ($model instanceof Model_Admin)
  50. {
  51. //do admin insert/update stuff here
  52. return;
  53. }
  54. parent::save($model);//call regular method
  55. }
  56. }
  57.  
  58. class BasicEntity
  59. {
  60.  
  61. protected $tablename;
  62.  
  63. protected $schema;
  64.  
  65. public function update()
  66. {
  67.  
  68. $fields = "";
  69. $placeholders = "";
  70.  
  71. foreach($this -> schema as $field => $type)
  72. {
  73.  
  74. // you join the fields here to get something like ('username', 'email', 'enabled', 'createdAt', 'password')
  75. // then you write your PDO statement providing placeholders like (:?, :?, :?, :?, :?)
  76. // you'll have to bind parameters based on their $type [int, string, date]
  77.  
  78. }
  79.  
  80. $query = sprintf(
  81. "UPDATE %s SET VALUES(%s) = %s",
  82. $this -> tablename,
  83. $fields,
  84. $placeholders
  85. );
  86.  
  87. // execute statement here, handle exceptions, and so...
  88.  
  89. }
  90.  
  91. }
  92.  
  93. class User extends BasicEntity
  94. {
  95.  
  96. protected $id;
  97. protected $username;
  98. protected $email;
  99. protected $password;
  100. protected $enabled;
  101. protected $createdAt;
  102.  
  103. public function __construct()
  104. {
  105.  
  106. $this -> tablename = '_user';
  107. $this -> schema = array(
  108. 'id' => 'int',
  109. 'username' => 'string',
  110. 'email' => 'string',
  111. 'password' => 'string',
  112. 'enabled' => 'int',
  113. 'createdAt' => 'datetime'
  114. );
  115.  
  116. }
  117.  
  118. }
  119.  
  120. class Admin extends User
  121. {
  122.  
  123. protected $additionalProperty;
  124.  
  125. public function __construct()
  126. {
  127.  
  128. parent::__construct();
  129.  
  130. $this -> schema['additionalProperty'] = 'string';
  131.  
  132. }
  133.  
  134. }
Add Comment
Please, Sign In to add comment