diff --git a/CHANGELOG.md b/CHANGELOG.md index 042688f..1217fb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,24 @@ 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). +## [4.0.0] - 2025-01-03 +### Changed +- `getMap()` is no longer abstract and returns an empty array by default, +so it is no longer necessary to implement for read-only APIs. +- If the ID column is returned from `getMap()`, it is now automatically writable +without having to set a `writableId` flag. +- `getSelectMap()` now returns an empty array by default (rather than the `getMap()` value), +so it is no longer necessary to implement this method just to prevent selecting write-only +properties when using `getSelectProps()`. + +> [!Important] +> When upgrading, remove readonly ID properties from `getMap()`, and implement +> `getSelectMap()` if any writable property is not returned by `getSelectProps()`. + +### Removed +- Unnecessary `getDefaultValues()` method. Defaults can be set via `processValues()` instead. +- `writableId` bool property. + ## [3.0.0] - 2024-10-29 ### Added - Official support for PostgreSQL. @@ -187,6 +205,7 @@ return early if passed an empty IDs array. - Initial stable release +[4.0.0]: https://github.com/devtheorem/phaster/compare/v3.0.0...v4.0.0 [3.0.0]: https://github.com/devtheorem/phaster/compare/v2.9.0...v3.0.0 [2.9.0]: https://github.com/devtheorem/phaster/compare/v2.8.0...v2.9.0 [2.8.0]: https://github.com/devtheorem/phaster/compare/v2.7.0...v2.8.0 diff --git a/src/Entities.php b/src/Entities.php index afa0075..eddc200 100644 --- a/src/Entities.php +++ b/src/Entities.php @@ -8,15 +8,8 @@ abstract class Entities { - /** - * Returns a map of properties to columns for the current table - * @return array - */ - abstract protected function getMap(): array; - protected PeachySql $db; public string $idField = 'id'; - protected bool $writableId = false; private string $idColumn; /** @var array */ private array $fullPropMap; @@ -37,25 +30,13 @@ public function __construct(PeachySql $db) $propMap = Helpers::propListToPropMap([...array_values($bcProps), ...$selectProps]); if (!isset($propMap[$this->idField])) { - throw new \Exception('Missing required id property in map'); - } - - $map = $this->getMap(); - - if (isset($map[$this->idField])) { - /** @psalm-suppress MixedAssignment */ - $this->idColumn = $map[$this->idField]; - - if (!$this->writableId) { - unset($map[$this->idField]); // prevent modifying identity column - } - } else { - $idParts = explode('.', $propMap[$this->idField]->col); - $this->idColumn = array_pop($idParts); + throw new \Exception("Missing required {$this->idField} property in select map"); } + $idParts = explode('.', $propMap[$this->idField]->col); + $this->idColumn = array_pop($idParts); $this->fullPropMap = $propMap; - $this->map = $map; + $this->map = $this->getMap(); } /** @@ -66,6 +47,15 @@ protected function getTableName(): string return (new \ReflectionClass($this))->getShortName(); } + /** + * Returns a map of properties to writable columns for the current table. + * @return array + */ + protected function getMap(): array + { + return []; + } + /** * Returns the identity increment value of the table */ @@ -99,12 +89,12 @@ protected function getDefaultSort(): array } /** - * Can be used to return a separate property map for filtering/sorting (but not inserting/updating) + * Returns a map of properties to columns for selecting/filtering/sorting. * @return array */ protected function getSelectMap(): array { - return $this->getMap(); + return []; } /** diff --git a/test/DbTestCase.php b/test/DbTestCase.php index a9cdf47..f90168f 100644 --- a/test/DbTestCase.php +++ b/test/DbTestCase.php @@ -322,7 +322,7 @@ public function testModernUsers(): void $db->insertRow('UserThings', ['user_id' => $ids[3]]); - $actual = $entities->getEntitiesByIds([$ids[2], $ids[3]], ['id', 'name', 'isDisabled', 'computed', 'weight', 'thing.uid']); + $actual = $entities->getEntitiesByIds([$ids[2], $ids[3]], ['id', 'name', 'isDisabled', 'computed', 'thing.uid']); $expected = [ [ @@ -330,7 +330,6 @@ public function testModernUsers(): void 'name' => 'Modern user 4', 'isDisabled' => false, 'computed' => 41.0, - 'weight' => $db->options->floatSelectedAsString ? '40' : 40.0, 'thing' => [ 'uid' => $ids[3], ], @@ -340,7 +339,6 @@ public function testModernUsers(): void 'name' => 'Modern user 3 modified', 'isDisabled' => false, 'computed' => 31.0, - 'weight' => $db->options->floatSelectedAsString ? '30' : 30.0, 'thing' => null, ], ]; diff --git a/test/src/ModernUsers.php b/test/src/ModernUsers.php index 7b52347..0fa730f 100644 --- a/test/src/ModernUsers.php +++ b/test/src/ModernUsers.php @@ -14,7 +14,6 @@ protected function getTableName(): string protected function getMap(): array { return [ - 'id' => 'user_id', 'name' => 'name', 'birthday' => 'dob', 'weight' => 'weight', @@ -31,11 +30,12 @@ protected function getSelectProps(): array return [ new Prop('id', 'u.user_id'), - new Prop('name', 'name', false, true, 'username'), - new Prop('isDisabled', 'is_disabled', false, true, '', 'bool'), - new Prop('computed', '', false, true, '', null, false, $getValue, ['weight']), + new Prop('name', 'name', alias: 'username'), + new Prop('weight', 'weight'), + new Prop('isDisabled', 'is_disabled', type: 'bool'), + new Prop('computed', getValue: $getValue, dependsOn: ['weight']), new Prop('thing.id', 'ut.thing_id', true), - new Prop('thing.uid', 'ut.user_id', false, true, 'thing_user'), + new Prop('thing.uid', 'ut.user_id', alias: 'thing_user'), ]; } diff --git a/test/src/Users.php b/test/src/Users.php index caf13f5..1f3fbed 100644 --- a/test/src/Users.php +++ b/test/src/Users.php @@ -10,7 +10,6 @@ class Users extends Entities protected function getMap(): array { return [ - 'id' => 'user_id', 'name' => 'name', 'birthday' => 'dob', 'weight' => 'weight', @@ -18,6 +17,11 @@ protected function getMap(): array ]; } + protected function getSelectMap(): array + { + return ['id' => 'user_id', ...$this->getMap()]; + } + protected function getSelectProps(): array { return [