1 | <?php |
||
27 | class Table extends AbstractCrud |
||
28 | { |
||
29 | use Db\Traits\TableProperty; |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | * |
||
34 | * @throws InvalidPrimaryKeyException |
||
35 | * @throws TableNotFoundException |
||
36 | */ |
||
37 | 26 | public function getPrimaryKey() : array |
|
41 | |||
42 | /** |
||
43 | * Get record from Db or create new object |
||
44 | * |
||
45 | * @param mixed $primary |
||
46 | * |
||
47 | * @return Db\RowInterface |
||
48 | * @throws TableNotFoundException |
||
49 | * @throws NotFoundException |
||
50 | */ |
||
51 | 4 | public function readOne($primary) |
|
67 | |||
68 | /** |
||
69 | * Get set of records |
||
70 | * |
||
71 | * @param int $offset |
||
72 | * @param int $limit |
||
73 | * @param array $params |
||
74 | * |
||
75 | * @return array[Row[], integer] |
||
|
|||
76 | * @throws TableNotFoundException |
||
77 | */ |
||
78 | 2 | public function readSet($offset = 0, $limit = 10, $params = []) |
|
79 | { |
||
80 | 2 | $select = $this->getTable()::select(); |
|
81 | |||
82 | // select only required fields |
||
83 | 2 | if (\count($this->getFields())) { |
|
84 | 2 | $fields = $this->getFields(); |
|
85 | 2 | $name = $this->getTable()->getName(); |
|
86 | 2 | $fields = array_map( |
|
87 | function ($field) use ($name) { |
||
88 | 2 | return $name .'.'. $field; |
|
89 | 2 | }, |
|
90 | 2 | $fields |
|
91 | ); |
||
92 | 2 | $select->select(implode(', ', $fields)); |
|
93 | } |
||
94 | |||
95 | // switch statement for DB type |
||
96 | 2 | $type = Proxy\Db::getOption('connect', 'type'); |
|
97 | switch ($type) { |
||
98 | 2 | case 'mysql': |
|
99 | 2 | $selectPart = $select->getSelect(); |
|
100 | 2 | $selectPart[0] = 'SQL_CALC_FOUND_ROWS ' . $selectPart[0]; |
|
101 | 2 | $select->select(...$selectPart); |
|
102 | 2 | $totalSQL = 'SELECT FOUND_ROWS()'; |
|
103 | 2 | break; |
|
104 | case 'pgsql': |
||
105 | default: |
||
106 | $selectTotal = clone $select; |
||
107 | $selectTotal->select('COUNT(*)'); |
||
108 | $totalSQL = $selectTotal->getSql(); |
||
109 | break; |
||
110 | } |
||
111 | |||
112 | 2 | $select->setLimit($limit); |
|
113 | 2 | $select->setOffset($offset); |
|
114 | |||
115 | 2 | $result = []; |
|
116 | 2 | $total = 0; |
|
117 | |||
118 | // run queries |
||
119 | // use transaction to avoid errors |
||
120 | 2 | Proxy\Db::transaction( |
|
121 | function () use (&$result, &$total, $select, $totalSQL) { |
||
122 | 2 | $result = $select->execute(); |
|
123 | 2 | $total = Proxy\Db::fetchOne($totalSQL); |
|
124 | 2 | } |
|
125 | ); |
||
126 | |||
127 | 2 | return [$result, $total]; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Create item |
||
132 | * |
||
133 | * @param array $data |
||
134 | * |
||
135 | * @return mixed |
||
136 | * @throws TableNotFoundException |
||
137 | */ |
||
138 | 1 | public function createOne($data) |
|
147 | |||
148 | /** |
||
149 | * Update item |
||
150 | * |
||
151 | * @param mixed $primary |
||
152 | * @param array $data |
||
153 | * |
||
154 | * @return integer |
||
155 | * @throws NotFoundException |
||
156 | * @throws TableNotFoundException |
||
157 | */ |
||
158 | 2 | public function updateOne($primary, $data) |
|
170 | |||
171 | /** |
||
172 | * Delete item |
||
173 | * |
||
174 | * @param mixed $primary |
||
175 | * |
||
176 | * @return integer |
||
177 | * @throws NotFoundException |
||
178 | * @throws TableNotFoundException |
||
179 | */ |
||
180 | 2 | public function deleteOne($primary) |
|
189 | } |
||
190 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.