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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. */
  40. public static function deleteById($id)
  41. {
  42. $data = self::_getModelById($id);
  43. if (is_null($data))
  44. return;
  45. $data->delete();
  46. }
  47. /**
  48. * @param $data LuDbo
  49. * @return int
  50. */
  51. public static function addId($data)
  52. {
  53. $data = static::getModel()->fromDBO($data);
  54. unset($data->id);
  55. $data->save();
  56. return $data->id;
  57. }
  58. /**
  59. * @param $data LuDbo
  60. */
  61. public static function add($data)
  62. {
  63. $data = static::getModel()->fromDBO($data);
  64. $data->save();
  65. }
  66. /**
  67. * @param $id int
  68. * @param $data LuDbo
  69. * @return LuDbo|null
  70. */
  71. public static function editById($id, $data)
  72. {
  73. return static::getModel()->fromDBO($data, self::_getModelById($id))->save();
  74. }
  75. /**
  76. * @param $predicates array
  77. * @param $orders array
  78. * @param int $page
  79. * @param int $perPage
  80. * @return LuMultipleDbo
  81. */
  82. public static function getMultiple($predicates, $orders, $page = 0, $perPage = PHP_INT_MAX)
  83. {
  84. $model = static::getModel();
  85. foreach($predicates as $predicate) {
  86. $model = $model->where($predicate[0], $predicate[1], $predicate[2]);
  87. }
  88. $count = $model->count();
  89. foreach($orders as $order) {
  90. $model = $model->orderBy($order[0], $order[1]);
  91. }
  92. $data = $model->take($perPage)->offset($page * $perPage)->get();
  93. $dbo = self::arrayToDbo($data);
  94. return new LuMultipleDbo($count, $dbo);
  95. }
  96. /**
  97. * @return LuModel
  98. */
  99. protected static function getModel()
  100. {
  101. return null;
  102. }
  103. /**
  104. * @return array
  105. */
  106. protected static function getOrderBy()
  107. {
  108. return array();
  109. }
  110. public static function getAll($page = 0, $perPage = PHP_INT_MAX)
  111. {
  112. return self::getMultiple(array(), static::getOrderBy(), $page, $perPage);
  113. }
  114. }