123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
-
- namespace Luticate\Utils;
-
- abstract class LuDataAccess {
-
- public function __construct()
- {
- }
-
- /**
- * @var LuModel
- */
- protected $model = null;
-
- /**
- * @param $page int
- * @param $perPage int
- * @param $column string
- * @param $order string
- * @param $query string
- * @return LuModel[]
- */
- protected function search_($page, $perPage, $column, $order, $query)
- {
- return $this->model;
- }
-
- /**
- * @param $data LuModel[]
- * @return LuDbo[]
- */
- protected function arrayToDbo($data)
- {
- $tab = [];
- foreach ($data as $q) {
- if (!is_null($q)) {
- $tab[] = $q->toDbo();
- }
- }
- return $tab;
- }
-
- /**
- * @param $page int
- * @param $perPage int
- * @param $column string
- * @param $order string
- * @param $query string
- * @return LuDbo[]
- */
- public function search($page, $perPage, $column, $order, $query = null)
- {
- if (is_null($query))
- $data = $this->model;
- else
- $data = $this->search_($page, $perPage, $column, $order, $query);
-
- $data = $data->orderBy($column, $order)->take($perPage)->offset($page * $perPage)->get();
-
- $tab = $this->arrayToDbo($data);
- return $tab;
- }
-
- /**
- * @param $id int
- * @return LuModel
- */
- protected function getModelById($id)
- {
- return $this->model->where('id', '=', $id)->first();
- }
-
- /**
- * @param $id int
- * @return LuDbo|null
- */
- public function getById($id)
- {
- $data = $this->getModelById($id);
- if (is_null($data))
- return null;
- return $data->toDbo();
- }
-
- /**
- * @param $id int
- */
- public function deleteById($id)
- {
- $data = $this->getModelById($id);
- if (is_null($data))
- return;
- $data->delete();
- }
-
- /**
- * @param $data LuDbo
- * @return int
- */
- public function addId($data)
- {
- $data = $this->model->fromDBO($data);
- $data->save();
- return $data->id;
- }
-
- /**
- * @param $data LuDbo
- */
- public function add($data)
- {
- $data = $this->model->fromDBO($data);
- $data->save();
- }
-
- /**
- * @param $id int
- * @param $data LuDbo
- * @return LuDbo|null
- */
- public function editById($id, $data)
- {
- return $this->model->fromDBO($data, $this->getModelById($id))->save();
- }
- }
|