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.3KB

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