Skip to content

Commit 34548fe

Browse files
authored
No explicit $table param in Array & Csv persistences (#656)
1 parent 7c953c6 commit 34548fe

File tree

4 files changed

+44
-46
lines changed

4 files changed

+44
-46
lines changed

docs/model.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ Populating Data
513513

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

518518

519519

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

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

43+
protected function makeFakeModelWithForeignTable(): Model
44+
{
45+
$modelCloned = clone $this->getOwner();
46+
$modelCloned->table = $this->foreign_table;
47+
48+
// @TODO hooks will be fixed on a cloned model, Join should be replaced later by supporting unioned table as a table model
49+
50+
return $modelCloned;
51+
}
52+
4353
/**
4454
* Called from afterLoad hook.
4555
*/
@@ -55,7 +65,7 @@ public function afterLoad(): void
5565

5666
try {
5767
$data = Persistence\Array_::assertInstanceOf($model->persistence)
58-
->load($model, $this->id, $this->foreign_table);
68+
->load($this->makeFakeModelWithForeignTable(), $this->id, $this->foreign_table);
5969
} catch (Exception $e) {
6070
throw (new Exception('Unable to load joined record', $e->getCode(), $e))
6171
->addMoreInfo('table', $this->foreign_table)
@@ -83,9 +93,8 @@ public function beforeInsert(array &$data): void
8393
$persistence = $this->persistence ?: $this->getOwner()->persistence;
8494

8595
$this->id = $persistence->insert(
86-
$this->getOwner(),
87-
$this->save_buffer,
88-
$this->foreign_table
96+
$this->makeFakeModelWithForeignTable(),
97+
$this->save_buffer
8998
);
9099

91100
$data[$this->master_field] = $this->id;
@@ -109,9 +118,8 @@ public function afterInsert($id): void
109118
$persistence = $this->persistence ?: $this->getOwner()->persistence;
110119

111120
$this->id = $persistence->insert(
112-
$this->getOwner(),
113-
$this->save_buffer,
114-
$this->foreign_table
121+
$this->makeFakeModelWithForeignTable(),
122+
$this->save_buffer
115123
);
116124
}
117125

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

129137
$this->id = $persistence->update(
130-
$this->getOwner(),
138+
$this->makeFakeModelWithForeignTable(),
131139
$this->id,
132140
$this->save_buffer,
133141
$this->foreign_table
@@ -148,9 +156,8 @@ public function doDelete($id): void
148156
$persistence = $this->persistence ?: $this->getOwner()->persistence;
149157

150158
$persistence->delete(
151-
$this->getOwner(),
152-
$this->id,
153-
$this->foreign_table
159+
$this->makeFakeModelWithForeignTable(),
160+
$this->id
154161
);
155162

156163
$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)