1 | <?php |
||
8 | abstract class SimpleCrud extends AbstractEntity implements EntityInterface |
||
9 | { |
||
10 | protected $searchFields; |
||
11 | |||
12 | abstract protected function getDatabase(); |
||
13 | |||
14 | protected function getQuery(SearchQuery $search = null, &$page = null) |
||
15 | { |
||
16 | $database = $this->getDatabase(); |
||
17 | $entity = $database->get($this->name); |
||
18 | |||
19 | if ($search === null) { |
||
20 | return $entity->select()->orderBy('id DESC'); |
||
21 | } |
||
22 | |||
23 | $page = $search->getPage() ?: 1; |
||
24 | |||
25 | $query = $entity->select() |
||
26 | ->orderBy("`{$entity->name}`.`id` DESC") |
||
27 | ->offset(($page * 50) - 50) |
||
28 | ->limit(50); |
||
29 | |||
30 | if ($this->searchFields === null) { |
||
31 | $this->searchFields = [$this->getFirstField()]; |
||
32 | } |
||
33 | |||
34 | foreach ($search->getWords() as $k => $word) { |
||
35 | foreach ($this->searchFields as $field) { |
||
36 | $query->where("`{$entity->name}`.`{$field}` LIKE :w{$k}", [":w{$k}" => "%{$word}%"]); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | foreach ($search->getConditions() as $name => $value) { |
||
41 | $related = $database->get($name)->select()->by('id', $value)->all(); |
||
42 | $query->relatedWith($related); |
||
43 | } |
||
44 | |||
45 | return $query; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | public function search(SearchQuery $search = null) |
||
52 | { |
||
53 | $query = $this->getQuery($search, $page); |
||
54 | |||
55 | $result = $query->all()->toArray(true); |
||
56 | |||
57 | if ($search && (count($result) === 50 || $page > 1)) { |
||
58 | $search->setPage($page); |
||
59 | } |
||
60 | |||
61 | return $result; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | public function create(array $data) |
||
68 | { |
||
69 | $entity = $this->getDatabase()->get($this->name); |
||
70 | |||
71 | return $entity->create($data)->save(false, true)->id; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function read($id) |
||
78 | { |
||
79 | $entity = $this->getDatabase()->get($this->name); |
||
80 | |||
81 | $row = $entity[$id]; |
||
82 | |||
83 | return $row ? $row->toArray() : null; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function update($id, array $data) |
||
90 | { |
||
91 | $entity = $this->getDatabase()->get($this->name); |
||
92 | |||
93 | return $entity[$id]->set($data)->save(false, true)->toArray(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function delete($id) |
||
100 | { |
||
101 | $entity = $this->getDatabase()->get($this->name); |
||
102 | |||
103 | unset($entity[$id]); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function getLabel($id, array $data) |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | protected function getFirstField() |
||
118 | { |
||
119 | $entity = $this->getDatabase()->get($this->name); |
||
120 | |||
127 | } |
||
128 |