您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuDataAccess.php 3.4KB

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