Skip to content

Commit

Permalink
Allow setting an existing ID in processValues to skip inserting a new…
Browse files Browse the repository at this point in the history
… row
  • Loading branch information
theodorejb committed Sep 10, 2024
1 parent 8315493 commit e349e0c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [2.9.0] - 2024-09-10
### Added
- `processValues()` now allows setting the ID of an existing row on the returned object, in which
case a new row will not be inserted, and the specified ID will be returned in the list of row IDs.

## [2.8.0] - 2023-12-22
### Added
Expand Down Expand Up @@ -151,7 +154,7 @@ return early if passed an empty IDs array.
### Changed
- Initial stable release

[Unreleased]: https://github.com/theodorejb/phaster/compare/v2.8.0...HEAD
[2.9.0]: https://github.com/theodorejb/phaster/compare/v2.8.0...v2.9.0
[2.8.0]: https://github.com/theodorejb/phaster/compare/v2.7.0...v2.8.0
[2.7.0]: https://github.com/theodorejb/phaster/compare/v2.6.0...v2.7.0
[2.6.0]: https://github.com/theodorejb/phaster/compare/v2.5.0...v2.6.0
Expand Down
29 changes: 23 additions & 6 deletions src/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,32 @@ public function addEntities(array $entities): array

$defaultValues = $this->getDefaultValues();
$rows = [];

foreach ($entities as $entity) {
$data = array_replace_recursive($defaultValues, $entity);
$row = Helpers::allPropertiesToColumns($this->map, $this->processValues($data, []));
$rows[] = $this->processRow($row, []);
$existingIds = [];

foreach ($entities as $key => $entity) {
unset($entity[$this->idField]); // any ID posted to API should be ignored
$entity = array_replace_recursive($defaultValues, $entity);
$entity = $this->processValues($entity, []);

// if processValues sets an ID for an existing item, don't insert a new row for it
if (isset($entity[$this->idField])) {
$id = $entity[$this->idField];
if (!is_int($id)) {
throw new \Exception('ID value set by processValues must be an integer');
}
$existingIds[$key] = $id;
} else {
$row = Helpers::allPropertiesToColumns($this->map, $entity);
$rows[] = $this->processRow($row, []);
}
}

try {
return $this->db->insertRows($this->getTableName(), $rows, $this->getIdentityIncrement())->getIds();
$ids = $this->db->insertRows($this->getTableName(), $rows, $this->getIdentityIncrement())->getIds();
foreach ($existingIds as $offset => $id) {
array_splice($ids, $offset, 0, [$id]);
}
return $ids;
} catch (SqlException $e) {
throw $this->properException($e);
}
Expand Down
2 changes: 2 additions & 0 deletions test/EntitiesDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ public function testModernUsers(Entities $entities, PeachySql $db): void
}

$ids = $entities->addEntities($users);
$this->assertSame(-42, $ids[1]); // manually set ID in processValues

$db->insertRow('UserThings', ['user_id' => $ids[3]]);

$actual = $entities->getEntitiesByIds([$ids[2], $ids[3]], ['id', 'name', 'isDisabled', 'computed', 'weight', 'thing.uid']);
Expand Down
8 changes: 6 additions & 2 deletions test/src/ModernUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ protected function getBaseQuery(QueryOptions $options): string

protected function processValues(array $data, array $ids): array
{
if (count($ids) === 0 && $data['name'] === 'Modern user 3') {
$data['name'] = 'Modern user 3 modified';
if (count($ids) === 0) {
if ($data['name'] === 'Modern user 3') {
$data['name'] = 'Modern user 3 modified';
} elseif ($data['name'] === 'Modern user 2') {
$data['id'] = -42; // don't insert row for this item
}
}

return $data;
Expand Down

0 comments on commit e349e0c

Please sign in to comment.