Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. <?php
  2. namespace EngineCoreDatabase;
  3.  
  4. class QueryBuilder
  5. {
  6. /**
  7. * @var array
  8. */
  9. protected $sql = [];
  10. /**
  11. * @var array
  12. */
  13. public $values = [];
  14. /**
  15. * @param string $fields
  16. * @return $this
  17. */
  18. public function select($fields = '*')
  19. {
  20. $this->reset();
  21. $this->sql['select'] = "SELECT {$fields} ";
  22. return $this;
  23. }
  24. /**
  25. * @return $this
  26. */
  27. public function delete()
  28. {
  29. $this->reset();
  30. $this->sql['delete'] = "DELETE ";
  31. return $this;
  32. }
  33. /**
  34. * @param $table
  35. * @return $this
  36. */
  37. public function from($table)
  38. {
  39. $this->sql['from'] = "FROM {$table} ";
  40. return $this;
  41. }
  42. /**
  43. * @param string $column
  44. * @param string $value
  45. * @param string $operator
  46. * @return $this
  47. */
  48. public function where($column, $value, $operator = '=')
  49. {
  50. $this->sql['where'][] = "{$column} {$operator} ?";
  51. $this->values[] = $value;
  52. return $this;
  53. }
  54. /**
  55. * @param $field
  56. * @param $order
  57. * @return $this
  58. */
  59. public function orderBy($field, $order)
  60. {
  61. $this->sql['order_by'] = "ORDER BY {$field} {$order}";
  62. return $this;
  63. }
  64. /**
  65. * @param $number
  66. * @return $this
  67. */
  68. public function limit($number)
  69. {
  70. $this->sql['limit'] = " LIMIT {$number}";
  71. return $this;
  72. }
  73. /**
  74. * @param $table
  75. * @return $this
  76. */
  77. public function update($table)
  78. {
  79. $this->reset();
  80. $this->sql['update'] = "UPDATE {$table} ";
  81. return $this;
  82. }
  83. public function insert($table)
  84. {
  85. $this->reset();
  86. $this->sql['insert'] = "INSERT INTO {$table} ";
  87. return $this;
  88. }
  89. /**
  90. * @param array $data
  91. * @return $this
  92. */
  93. public function set($data = [])
  94. {
  95. $this->sql['set'] .= "SET ";
  96. if(!empty($data)) {
  97. foreach ($data as $key => $value) {
  98. $this->sql['set'] .= "{$key} = ?";
  99. if (next($data)) {
  100. $this->sql['set'] .= ", ";
  101. }
  102. $this->values[] = $value;
  103. }
  104. }
  105. return $this;
  106. }
  107. /**
  108. * @return string
  109. */
  110. public function sql()
  111. {
  112. $sql = '';
  113. if(!empty($this->sql)) {
  114. foreach ($this->sql as $key => $value) {
  115. if ($key == 'where') {
  116. $sql .= ' WHERE ';
  117. foreach ($value as $where) {
  118. $sql .= $where;
  119. if (count($value) > 1 and next($value)) {
  120. $sql .= ' AND ';
  121. }
  122. }
  123. } else {
  124. $sql .= $value;
  125. }
  126. }
  127. }
  128. return $sql;
  129. }
  130. /**
  131. * Reset Builder
  132. */
  133. public function reset()
  134. {
  135. $this->sql = [];
  136. $this->values = [];
  137. }
  138. }
  139.  
  140. <?php
  141.  
  142. namespace AdminModelUser;
  143.  
  144. use EngineCoreDatabaseActiveRecord;
  145.  
  146. class User
  147. {
  148. use ActiveRecord;
  149.  
  150. /**
  151. * @var string
  152. */
  153. public $table = 'user';
  154.  
  155. /**
  156. * @var User id
  157. */
  158. public $id;
  159.  
  160. /**
  161. * @var User email
  162. */
  163. public $email;
  164.  
  165. /**
  166. * @var User password
  167. */
  168. public $password;
  169.  
  170. /**
  171. * @var User role
  172. */
  173. public $role;
  174.  
  175. /**
  176. * @var User hash
  177. */
  178. public $hash;
  179.  
  180. /**
  181. * @var User date_reg
  182. */
  183. public $date_reg;
  184.  
  185. /**
  186. * @return mixed
  187. */
  188. public function getId()
  189. {
  190. return $this->id;
  191. }
  192.  
  193. /**
  194. * @param $id
  195. */
  196. public function setId($id)
  197. {
  198. $this->id = $id;
  199. }
  200.  
  201. /**
  202. * @return string
  203. */
  204. public function getEmail()
  205. {
  206. return $this->email;
  207. }
  208.  
  209. /**
  210. * @param User $email
  211. */
  212. public function setEmail($email)
  213. {
  214. $this->email = $email;
  215. }
  216.  
  217. /**
  218. * @return string
  219. */
  220. public function getPassword()
  221. {
  222. return $this->password;
  223. }
  224.  
  225. /**
  226. * @param User $password
  227. */
  228. public function setPassword($password)
  229. {
  230. $this->password = $password;
  231. }
  232.  
  233. /**
  234. * @return string
  235. */
  236. public function getRole()
  237. {
  238. return $this->role;
  239. }
  240.  
  241. /**
  242. * @param User $role
  243. */
  244. public function setRole($role)
  245. {
  246. $this->role = $role;
  247. }
  248.  
  249. /**
  250. * @return string
  251. */
  252. public function getHash()
  253. {
  254. return $this->hash;
  255. }
  256.  
  257. /**
  258. * @param User $hash
  259. */
  260. public function setHash($hash)
  261. {
  262. $this->hash = $hash;
  263. }
  264.  
  265. /**
  266. * @return mixed
  267. */
  268. public function getDateReg()
  269. {
  270. return $this->date_reg;
  271. }
  272.  
  273. /**
  274. * @param mixed $date_reg
  275. */
  276. public function setDateReg($date_reg)
  277. {
  278. $this->date_reg = $date_reg;
  279. }
  280. }
  281.  
  282. <?php
  283.  
  284. namespace EngineCoreDatabase;
  285.  
  286. use ReflectionClass;
  287. use ReflectionProperty;
  288.  
  289. trait ActiveRecord
  290. {
  291. /**
  292. * @var Connection
  293. */
  294. protected $db;
  295.  
  296. /**
  297. * @var QueryBuilder
  298. */
  299. protected $queryBuilder;
  300.  
  301. /**
  302. * ActiveRecord constructor.
  303. * @param int $id
  304. */
  305. public function __construct($id = 0)
  306. {
  307. global $di;
  308.  
  309. $this->db = $di->get('db');
  310. $this->queryBuilder = new QueryBuilder();
  311.  
  312. if ($id) {
  313. $this->setId($id);
  314. }
  315. }
  316.  
  317. /**
  318. * @return string
  319. */
  320. public function getTable()
  321. {
  322. return $this->table;
  323. }
  324.  
  325. /**
  326. * Save User
  327. */
  328. public function save() {
  329. $properties = $this->getIssetProperties();
  330.  
  331. try {
  332. if (isset($this->id)) {
  333. $this->db->execute(
  334. $this->queryBuilder->update($this->getTable())
  335. ->set($properties)
  336. ->where('id', $this->id)
  337. ->sql(),
  338. $this->queryBuilder->values
  339. );
  340. } else {
  341. $this->db->execute(
  342. $this->queryBuilder->insert($this->getTable())
  343. ->set($properties)
  344. ->sql(),
  345. $this->queryBuilder->values
  346. );
  347. }
  348. } catch (Exception $e) {
  349. echo $e->getMessage();
  350. }
  351. }
  352.  
  353. /**
  354. * @return array
  355. */
  356. private function getIssetProperties()
  357. {
  358. $properties = [];
  359.  
  360. foreach ($this->getProperties() as $key => $property) {
  361. if (isset($this->{$property->getName()})) {
  362. $properties[$property->getName()] = $this->{$property->getName()};
  363. }
  364. }
  365.  
  366. return $properties;
  367. }
  368.  
  369. /**
  370. * @return ReflectionProperty[]
  371. */
  372. private function getProperties()
  373. {
  374. $reflection = new ReflectionClass($this);
  375. $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
  376.  
  377. return $properties;
  378. }
  379. }
  380.  
  381. <?php
  382.  
  383. namespace AdminModelUser;
  384.  
  385. use EngineModel;
  386.  
  387. class UserRepository extends Model
  388. {
  389. public function getUsers()
  390. {
  391. $sql = $this->queryBuilder->select()
  392. ->from('user')
  393. ->orderBy('id','DESC')
  394. ->sql();
  395.  
  396. return $this->db->query($sql);
  397. }
  398. public function test()
  399. {
  400. $user = new User(1);
  401. $user->setEmail("admin1@admin.com");
  402. $user->save();
  403. }
  404. }
  405.  
  406. <?php
  407.  
  408. namespace AdminController;
  409.  
  410. class DashboardController extends AdminController
  411. {
  412. public function index(){
  413.  
  414. $userModel = $this->load->model('User');
  415.  
  416. $userModel->repository->test();
  417.  
  418. print_r($userModel->repository->getUsers());
  419.  
  420. $this->view->render('dashboard');
  421. }
  422. }
  423.  
  424. $this->sql['set'] .= "SET ";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement