Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LuDataAccess.php 3.3KB

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