You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuDataAccess.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Luticate\Utils;
  3. abstract class LuDataAccess {
  4. public function __construct()
  5. {
  6. }
  7. public static function __callStatic($name, $arguments)
  8. {
  9. $class = get_called_class();
  10. $me = new $class();
  11. return call_user_func_array(array($me, '_' . $name), $arguments);
  12. }
  13. /**
  14. * @var LuModel
  15. */
  16. protected $model = null;
  17. /**
  18. * @param $page int
  19. * @param $perPage int
  20. * @param $column string
  21. * @param $order string
  22. * @param $query string
  23. * @return LuModel[]
  24. */
  25. protected function search_($page, $perPage, $column, $order, $query)
  26. {
  27. return $this->model;
  28. }
  29. /**
  30. * @param $data LuModel[]
  31. * @return LuDbo[]
  32. */
  33. protected static function arrayToDbo($data)
  34. {
  35. $tab = [];
  36. foreach ($data as $q) {
  37. if (!is_null($q)) {
  38. $tab[] = $q->toDbo();
  39. }
  40. }
  41. return $tab;
  42. }
  43. /**
  44. * @param $page int
  45. * @param $perPage int
  46. * @param $column string
  47. * @param $order string
  48. * @param $query string
  49. * @return LuDbo[]
  50. */
  51. public function search($page, $perPage, $column, $order, $query = null)
  52. {
  53. if (is_null($query))
  54. $data = $this->model;
  55. else
  56. $data = $this->search_($page, $perPage, $column, $order, $query);
  57. $data = $data->orderBy($column, $order)->take($perPage)->offset($page * $perPage)->get();
  58. $tab = $this->arrayToDbo($data);
  59. return $tab;
  60. }
  61. /**
  62. * @param $id int
  63. * @return LuModel
  64. */
  65. protected function _getModelById($id)
  66. {
  67. return $this->model->where('id', '=', $id)->first();
  68. }
  69. /**
  70. * @param $id int
  71. * @return LuDbo|null
  72. */
  73. public function _getById($id)
  74. {
  75. $data = $this->_getModelById($id);
  76. if (is_null($data))
  77. return null;
  78. return $data->toDbo();
  79. }
  80. /**
  81. * @param $id int
  82. */
  83. public function deleteById($id)
  84. {
  85. $data = $this->_getModelById($id);
  86. if (is_null($data))
  87. return;
  88. $data->delete();
  89. }
  90. /**
  91. * @param $data LuDbo
  92. * @return int
  93. */
  94. public function _addId($data)
  95. {
  96. $data = $this->model->fromDBO($data);
  97. $data->save();
  98. return $data->id;
  99. }
  100. /**
  101. * @param $data LuDbo
  102. */
  103. public function _add($data)
  104. {
  105. $data = $this->model->fromDBO($data);
  106. $data->save();
  107. }
  108. /**
  109. * @param $id int
  110. * @param $data LuDbo
  111. * @return LuDbo|null
  112. */
  113. public function _editById($id, $data)
  114. {
  115. return $this->model->fromDBO($data, $this->_getModelById($id))->save();
  116. }
  117. /**
  118. * @param $predicates array
  119. * @param $orders array
  120. * @param int $page
  121. * @param int $perPage
  122. * @return array
  123. */
  124. public function _getMultiple($predicates, $orders, $page = 0, $perPage = PHP_INT_MAX)
  125. {
  126. $model = $this->model;
  127. foreach($predicates as $predicate)
  128. {
  129. $model = $model->where($predicate[0], $predicate[1], $predicate[2]);
  130. }
  131. $count = $model->count();
  132. foreach($orders as $order)
  133. {
  134. $model = $model->orderBy($order[0], $order[1]);
  135. }
  136. $data = $model->take($perPage)->offset($page * $perPage)->get();
  137. $dbo = self::arrayToDbo($data);
  138. return array("Count" => $count, "Data" => $dbo);
  139. }
  140. }