Skip to content

Commit 525ab5e

Browse files
committed
WIP remove explicit table everywhere
1 parent 303be75 commit 525ab5e

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

docs/model.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ Populating Data
532532

533533
The method will still convert the data needed and operate with joined
534534
tables as needed. If you wish to access tables directly, you'll have to look
535-
into Persistence::insert($m, $data, $table);
535+
into Persistence::insert($m, $data);
536536

537537

538538

src/Persistence/Array_.php

+33-22
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private function assertNoIdMismatch($idFromRow, $id): void
5757
}
5858
}
5959

60-
private function saveRow(Model $model, array $row, $id, string $table): void
60+
private function saveRow(Model $model, array $row, $id): void
6161
{
6262
if ($model->id_field) {
6363
$idField = $model->getField($model->id_field);
@@ -68,7 +68,7 @@ private function saveRow(Model $model, array $row, $id, string $table): void
6868
}
6969
}
7070

71-
$this->data[$table][$id] = $row;
71+
$this->data[$model->table][$id] = $row;
7272
}
7373

7474
private function addIdToLoadRow(Model $model, array &$row, $id): void
@@ -140,34 +140,37 @@ public function add(Model $model, array $defaults = []): Model
140140

141141
public function tryLoad(Model $model, $id, string $table = null): ?array
142142
{
143-
$table = $table ?? $model->table;
144-
if (!isset($this->data[$table])) {
143+
if ($table !== null) {
144+
throw new \Error('debug xxx!! ' . $table . ' vs. ' . $model->table);
145+
}
146+
147+
if (!isset($this->data[$model->table])) {
145148
throw (new Exception('Table was not found in the array data source'))
146-
->addMoreInfo('table', $table);
149+
->addMoreInfo('table', $model->table);
147150
}
148151

149152
if ($id === self::ID_LOAD_ONE || $id === self::ID_LOAD_ANY) {
150-
if (count($this->data[$table]) === 0) {
153+
if (count($this->data[$model->table]) === 0) {
151154
return null;
152-
} elseif ($id === self::ID_LOAD_ONE && count($this->data[$table]) !== 1) {
155+
} elseif ($id === self::ID_LOAD_ONE && count($this->data[$model->table]) !== 1) {
153156
throw (new Exception('Ambiguous conditions, more than one record can be loaded.'))
154157
->addMoreInfo('model', $model)
155158
->addMoreInfo('id', null);
156159
}
157160

158-
$id = array_key_first($this->data[$table]);
161+
$id = array_key_first($this->data[$model->table]);
159162

160-
$row = $this->tryLoad($model, $id, $table);
163+
$row = $this->tryLoad($model, $id);
161164
$model->setId($id); // @TODO is it needed?
162165
163166
return $row;
164167
}
165168

166-
if (!isset($this->data[$table][$id])) {
169+
if (!isset($this->data[$model->table][$id])) {
167170
return null;
168171
}
169172

170-
$row = $this->data[$table][$id];
173+
$row = $this->data[$model->table][$id];
171174
$this->addIdToLoadRow($model, $row, $id);
172175

173176
return $this->typecastLoadRow($model, $row);
@@ -180,13 +183,15 @@ public function tryLoad(Model $model, $id, string $table = null): ?array
180183
*/
181184
public function insert(Model $model, array $data, string $table = null)
182185
{
183-
$table = $table ?? $model->table;
186+
if ($table !== null) {
187+
throw new \Error('debug xxx!! ' . $table . ' vs. ' . $model->table);
188+
}
184189

185190
$data = $this->typecastSaveRow($model, $data);
186191

187-
$id = $data[$model->id_field] ?? $this->generateNewId($model, $table);
192+
$id = $data[$model->id_field] ?? $this->generateNewId($model);
188193

189-
$this->saveRow($model, $data, $id, $table);
194+
$this->saveRow($model, $data, $id);
190195

191196
return $id;
192197
}
@@ -200,11 +205,13 @@ public function insert(Model $model, array $data, string $table = null)
200205
*/
201206
public function update(Model $model, $id, array $data, string $table = null)
202207
{
203-
$table = $table ?? $model->table;
208+
if ($table !== null) {
209+
throw new \Error('debug xxx!! ' . $table . ' vs. ' . $model->table);
210+
}
204211

205212
$data = $this->typecastSaveRow($model, $data);
206213

207-
$this->saveRow($model, array_merge($this->data[$table][$id] ?? [], $data), $id, $table);
214+
$this->saveRow($model, array_merge($this->data[$model->table][$id] ?? [], $data), $id);
208215

209216
return $id;
210217
}
@@ -216,9 +223,11 @@ public function update(Model $model, $id, array $data, string $table = null)
216223
*/
217224
public function delete(Model $model, $id, string $table = null)
218225
{
219-
$table = $table ?? $model->table;
226+
if ($table !== null) {
227+
throw new \Error('debug xxx!! ' . $table . ' vs. ' . $model->table);
228+
}
220229

221-
unset($this->data[$table][$id]);
230+
unset($this->data[$model->table][$id]);
222231
}
223232

224233
/**
@@ -228,13 +237,15 @@ public function delete(Model $model, $id, string $table = null)
228237
*/
229238
public function generateNewId(Model $model, string $table = null)
230239
{
231-
$table = $table ?? $model->table;
240+
if ($table !== null) {
241+
throw new \Error('debug xxx!! ' . $table . ' vs. ' . $model->table);
242+
}
232243

233244
$type = $model->id_field ? $model->getField($model->id_field)->type : 'integer';
234245

235246
switch ($type) {
236247
case 'integer':
237-
$ids = $model->id_field ? array_keys($this->data[$table]) : [count($this->data[$table])];
248+
$ids = $model->id_field ? array_keys($this->data[$model->table]) : [count($this->data[$model->table])];
238249

239250
$id = $ids ? max($ids) + 1 : 1;
240251

@@ -248,7 +259,7 @@ public function generateNewId(Model $model, string $table = null)
248259
->addMoreInfo('type', $type);
249260
}
250261

251-
$this->lastInsertIds[$table] = $id;
262+
$this->lastInsertIds[$model->table] = $id;
252263
$this->lastInsertIds['$'] = $id;
253264

254265
return $id;
@@ -310,7 +321,7 @@ public function initAction(Model $model, array $fields = null): \Atk4\Data\Actio
310321
}
311322

312323
/**
313-
* Will set limit defined inside $m onto data.
324+
* Will set limit defined inside $model onto data.
314325
*/
315326
protected function setLimitOrder(Model $model, \Atk4\Data\Action\Iterator $action)
316327
{

src/Persistence/Csv.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public function insert(Model $model, array $data)
326326
*
327327
* @param mixed $id
328328
*/
329-
public function update(Model $model, $id, array $data, string $table = null)
329+
public function update(Model $model, $id, array $data)
330330
{
331331
throw new Exception('Updating records is not supported in CSV persistence.');
332332
}
@@ -336,7 +336,7 @@ public function update(Model $model, $id, array $data, string $table = null)
336336
*
337337
* @param mixed $id
338338
*/
339-
public function delete(Model $model, $id, string $table = null)
339+
public function delete(Model $model, $id)
340340
{
341341
throw new Exception('Deleting records is not supported in CSV persistence.');
342342
}
@@ -346,8 +346,10 @@ public function delete(Model $model, $id, string $table = null)
346346
*
347347
* @return string
348348
*/
349-
public function generateNewId(Model $model, string $table = null)
349+
public function generateNewId(Model $model)
350350
{
351+
$ids = array_keys($this->data[$model->table]);
352+
351353
throw new Exception('Not implemented');
352354
}
353355

0 commit comments

Comments
 (0)