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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. $data->save();
  85. return $data->id;
  86. }
  87. /**
  88. * @param $data LuDbo
  89. */
  90. public static function add($data)
  91. {
  92. $data = static::getModel()->fromDBO($data);
  93. $data->save();
  94. }
  95. /**
  96. * @param $id int
  97. * @param $data LuDbo
  98. * @return LuDbo|null
  99. */
  100. public static function editById($id, $data)
  101. {
  102. return static::getModel()->fromDBO($data, self::_getModelById($id))->save();
  103. }
  104. /**
  105. * @param $predicates array
  106. * @param $orders array
  107. * @param int $page
  108. * @param int $perPage
  109. * @return array
  110. */
  111. public static function getMultiple($predicates, $orders, $page = 0, $perPage = PHP_INT_MAX)
  112. {
  113. $model = static::getModel();
  114. foreach($predicates as $predicate)
  115. {
  116. $model = $model->where($predicate[0], $predicate[1], $predicate[2]);
  117. }
  118. $count = $model->count();
  119. foreach($orders as $order)
  120. {
  121. $model = $model->orderBy($order[0], $order[1]);
  122. }
  123. $data = $model->take($perPage)->offset($page * $perPage)->get();
  124. $dbo = self::arrayToDbo($data);
  125. return new LuMultipleDbo($count, $dbo);
  126. }
  127. /**
  128. * @return LuModel
  129. */
  130. private static function getModel()
  131. {
  132. return null;
  133. }
  134. }