Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No explicit $table param in Array & Csv persistences #656

Merged
merged 2 commits into from
Apr 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/model.rst
Original file line number Diff line number Diff line change
@@ -532,7 +532,7 @@ Populating Data

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



53 changes: 22 additions & 31 deletions src/Persistence/Array_.php
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ private function assertNoIdMismatch($idFromRow, $id): void
}
}

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

$this->data[$table][$id] = $row;
$this->data[$model->table][$id] = $row;
}

private function addIdToLoadRow(Model $model, array &$row, $id): void
@@ -138,36 +138,35 @@ public function add(Model $model, array $defaults = []): Model
return $model;
}

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

if ($id === self::ID_LOAD_ONE || $id === self::ID_LOAD_ANY) {
if (count($this->data[$table]) === 0) {
if (count($this->data[$model->table]) === 0) {
return null;
} elseif ($id === self::ID_LOAD_ONE && count($this->data[$table]) !== 1) {
} elseif ($id === self::ID_LOAD_ONE && count($this->data[$model->table]) !== 1) {
throw (new Exception('Ambiguous conditions, more than one record can be loaded.'))
->addMoreInfo('model', $model)
->addMoreInfo('id', null);
}

$id = array_key_first($this->data[$table]);
$id = array_key_first($this->data[$model->table]);

$row = $this->tryLoad($model, $id, $table);
$row = $this->tryLoad($model, $id);
$model->setId($id); // @TODO is it needed?
return $row;
}

if (!isset($this->data[$table][$id])) {
if (!isset($this->data[$model->table][$id])) {
return null;
}

$row = $this->data[$table][$id];
$row = $this->data[$model->table][$id];
$this->addIdToLoadRow($model, $row, $id);

return $this->typecastLoadRow($model, $row);
@@ -178,15 +177,13 @@ public function tryLoad(Model $model, $id, string $table = null): ?array
*
* @return mixed
*/
public function insert(Model $model, array $data, string $table = null)
public function insert(Model $model, array $data)
{
$table = $table ?? $model->table;

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

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

$this->saveRow($model, $data, $id, $table);
$this->saveRow($model, $data, $id);

return $id;
}
@@ -198,13 +195,11 @@ public function insert(Model $model, array $data, string $table = null)
*
* @return mixed
*/
public function update(Model $model, $id, array $data, string $table = null)
public function update(Model $model, $id, array $data)
{
$table = $table ?? $model->table;

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

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

return $id;
}
@@ -214,27 +209,23 @@ public function update(Model $model, $id, array $data, string $table = null)
*
* @param mixed $id
*/
public function delete(Model $model, $id, string $table = null)
public function delete(Model $model, $id)
{
$table = $table ?? $model->table;

unset($this->data[$table][$id]);
unset($this->data[$model->table][$id]);
}

/**
* Generates new record ID.
*
* @return string
*/
public function generateNewId(Model $model, string $table = null)
public function generateNewId(Model $model)
{
$table = $table ?? $model->table;

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

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

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

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

$this->lastInsertIds[$table] = $id;
$this->lastInsertIds[$model->table] = $id;
$this->lastInsertIds['$'] = $id;

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

/**
* Will set limit defined inside $m onto data.
* Will set limit defined inside $model onto data.
*/
protected function setLimitOrder(Model $model, \Atk4\Data\Action\Iterator $action)
{
29 changes: 18 additions & 11 deletions src/Persistence/Array_/Join.php
Original file line number Diff line number Diff line change
@@ -40,6 +40,16 @@ protected function init(): void
}
}

protected function makeFakeModelWithForeignTable(): Model
{
$modelCloned = clone $this->getOwner();
$modelCloned->table = $this->foreign_table;

// @TODO hooks will be fixed on a cloned model, Join should be replaced later by supporting unioned table as a table model

return $modelCloned;
}

/**
* Called from afterLoad hook.
*/
@@ -55,7 +65,7 @@ public function afterLoad(): void

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

$this->id = $persistence->insert(
$this->getOwner(),
$this->save_buffer,
$this->foreign_table
$this->makeFakeModelWithForeignTable(),
$this->save_buffer
);

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

$this->id = $persistence->insert(
$this->getOwner(),
$this->save_buffer,
$this->foreign_table
$this->makeFakeModelWithForeignTable(),
$this->save_buffer
);
}

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

$this->id = $persistence->update(
$this->getOwner(),
$this->makeFakeModelWithForeignTable(),
$this->id,
$this->save_buffer,
$this->foreign_table
@@ -148,9 +156,8 @@ public function doDelete($id): void
$persistence = $this->persistence ?: $this->getOwner()->persistence;

$persistence->delete(
$this->getOwner(),
$this->id,
$this->foreign_table
$this->makeFakeModelWithForeignTable(),
$this->id
);

$this->id = null;
6 changes: 3 additions & 3 deletions src/Persistence/Csv.php
Original file line number Diff line number Diff line change
@@ -326,7 +326,7 @@ public function insert(Model $model, array $data)
*
* @param mixed $id
*/
public function update(Model $model, $id, array $data, string $table = null)
public function update(Model $model, $id, array $data)
{
throw new Exception('Updating records is not supported in CSV persistence.');
}
@@ -336,7 +336,7 @@ public function update(Model $model, $id, array $data, string $table = null)
*
* @param mixed $id
*/
public function delete(Model $model, $id, string $table = null)
public function delete(Model $model, $id)
{
throw new Exception('Deleting records is not supported in CSV persistence.');
}
@@ -346,7 +346,7 @@ public function delete(Model $model, $id, string $table = null)
*
* @return string
*/
public function generateNewId(Model $model, string $table = null)
public function generateNewId(Model $model)
{
throw new Exception('Not implemented');
}