Skip to content

Commit e58a7c5

Browse files
committed
Remove explicit table param from Persistence everywhere
1 parent 303be75 commit e58a7c5

File tree

4 files changed

+42
-46
lines changed

4 files changed

+42
-46
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

+22-31
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
@@ -138,36 +138,35 @@ public function add(Model $model, array $defaults = []): Model
138138
return $model;
139139
}
140140

141-
public function tryLoad(Model $model, $id, string $table = null): ?array
141+
public function tryLoad(Model $model, $id): ?array
142142
{
143-
$table = $table ?? $model->table;
144-
if (!isset($this->data[$table])) {
143+
if (!isset($this->data[$model->table])) {
145144
throw (new Exception('Table was not found in the array data source'))
146-
->addMoreInfo('table', $table);
145+
->addMoreInfo('table', $model->table);
147146
}
148147

149148
if ($id === self::ID_LOAD_ONE || $id === self::ID_LOAD_ANY) {
150-
if (count($this->data[$table]) === 0) {
149+
if (count($this->data[$model->table]) === 0) {
151150
return null;
152-
} elseif ($id === self::ID_LOAD_ONE && count($this->data[$table]) !== 1) {
151+
} elseif ($id === self::ID_LOAD_ONE && count($this->data[$model->table]) !== 1) {
153152
throw (new Exception('Ambiguous conditions, more than one record can be loaded.'))
154153
->addMoreInfo('model', $model)
155154
->addMoreInfo('id', null);
156155
}
157156

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

160-
$row = $this->tryLoad($model, $id, $table);
159+
$row = $this->tryLoad($model, $id);
161160
$model->setId($id); // @TODO is it needed?
162161
163162
return $row;
164163
}
165164

166-
if (!isset($this->data[$table][$id])) {
165+
if (!isset($this->data[$model->table][$id])) {
167166
return null;
168167
}
169168

170-
$row = $this->data[$table][$id];
169+
$row = $this->data[$model->table][$id];
171170
$this->addIdToLoadRow($model, $row, $id);
172171

173172
return $this->typecastLoadRow($model, $row);
@@ -178,15 +177,13 @@ public function tryLoad(Model $model, $id, string $table = null): ?array
178177
*
179178
* @return mixed
180179
*/
181-
public function insert(Model $model, array $data, string $table = null)
180+
public function insert(Model $model, array $data)
182181
{
183-
$table = $table ?? $model->table;
184-
185182
$data = $this->typecastSaveRow($model, $data);
186183

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

189-
$this->saveRow($model, $data, $id, $table);
186+
$this->saveRow($model, $data, $id);
190187

191188
return $id;
192189
}
@@ -198,13 +195,11 @@ public function insert(Model $model, array $data, string $table = null)
198195
*
199196
* @return mixed
200197
*/
201-
public function update(Model $model, $id, array $data, string $table = null)
198+
public function update(Model $model, $id, array $data)
202199
{
203-
$table = $table ?? $model->table;
204-
205200
$data = $this->typecastSaveRow($model, $data);
206201

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

209204
return $id;
210205
}
@@ -214,27 +209,23 @@ public function update(Model $model, $id, array $data, string $table = null)
214209
*
215210
* @param mixed $id
216211
*/
217-
public function delete(Model $model, $id, string $table = null)
212+
public function delete(Model $model, $id)
218213
{
219-
$table = $table ?? $model->table;
220-
221-
unset($this->data[$table][$id]);
214+
unset($this->data[$model->table][$id]);
222215
}
223216

224217
/**
225218
* Generates new record ID.
226219
*
227220
* @return string
228221
*/
229-
public function generateNewId(Model $model, string $table = null)
222+
public function generateNewId(Model $model)
230223
{
231-
$table = $table ?? $model->table;
232-
233224
$type = $model->id_field ? $model->getField($model->id_field)->type : 'integer';
234225

235226
switch ($type) {
236227
case 'integer':
237-
$ids = $model->id_field ? array_keys($this->data[$table]) : [count($this->data[$table])];
228+
$ids = $model->id_field ? array_keys($this->data[$model->table]) : [count($this->data[$model->table])];
238229

239230
$id = $ids ? max($ids) + 1 : 1;
240231

@@ -248,7 +239,7 @@ public function generateNewId(Model $model, string $table = null)
248239
->addMoreInfo('type', $type);
249240
}
250241

251-
$this->lastInsertIds[$table] = $id;
242+
$this->lastInsertIds[$model->table] = $id;
252243
$this->lastInsertIds['$'] = $id;
253244

254245
return $id;
@@ -310,7 +301,7 @@ public function initAction(Model $model, array $fields = null): \Atk4\Data\Actio
310301
}
311302

312303
/**
313-
* Will set limit defined inside $m onto data.
304+
* Will set limit defined inside $model onto data.
314305
*/
315306
protected function setLimitOrder(Model $model, \Atk4\Data\Action\Iterator $action)
316307
{

src/Persistence/Array_/Join.php

+16-11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ protected function init(): void
4040
}
4141
}
4242

43+
protected function makeFakeModelWithForeignTable(): Model
44+
{
45+
$model = $this->getOwner()->duplicate();
46+
$model->table = $this->foreign_table;
47+
48+
return $model;
49+
}
50+
4351
/**
4452
* Called from afterLoad hook.
4553
*/
@@ -55,7 +63,7 @@ public function afterLoad(): void
5563

5664
try {
5765
$data = Persistence\Array_::assertInstanceOf($model->persistence)
58-
->load($model, $this->id, $this->foreign_table);
66+
->load($this->makeFakeModelWithForeignTable(), $this->id, $this->foreign_table);
5967
} catch (Exception $e) {
6068
throw (new Exception('Unable to load joined record', $e->getCode(), $e))
6169
->addMoreInfo('table', $this->foreign_table)
@@ -83,9 +91,8 @@ public function beforeInsert(array &$data): void
8391
$persistence = $this->persistence ?: $this->getOwner()->persistence;
8492

8593
$this->id = $persistence->insert(
86-
$this->getOwner(),
87-
$this->save_buffer,
88-
$this->foreign_table
94+
$this->makeFakeModelWithForeignTable(),
95+
$this->save_buffer
8996
);
9097

9198
$data[$this->master_field] = $this->id;
@@ -109,9 +116,8 @@ public function afterInsert($id): void
109116
$persistence = $this->persistence ?: $this->getOwner()->persistence;
110117

111118
$this->id = $persistence->insert(
112-
$this->getOwner(),
113-
$this->save_buffer,
114-
$this->foreign_table
119+
$this->makeFakeModelWithForeignTable(),
120+
$this->save_buffer
115121
);
116122
}
117123

@@ -127,7 +133,7 @@ public function beforeUpdate(array &$data): void
127133
$persistence = $this->persistence ?: $this->getOwner()->persistence;
128134

129135
$this->id = $persistence->update(
130-
$this->getOwner(),
136+
$this->makeFakeModelWithForeignTable(),
131137
$this->id,
132138
$this->save_buffer,
133139
$this->foreign_table
@@ -148,9 +154,8 @@ public function doDelete($id): void
148154
$persistence = $this->persistence ?: $this->getOwner()->persistence;
149155

150156
$persistence->delete(
151-
$this->getOwner(),
152-
$this->id,
153-
$this->foreign_table
157+
$this->makeFakeModelWithForeignTable(),
158+
$this->id
154159
);
155160

156161
$this->id = null;

src/Persistence/Csv.php

+3-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,7 +346,7 @@ 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
{
351351
throw new Exception('Not implemented');
352352
}

0 commit comments

Comments
 (0)