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

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