From 00f6580fe34d228505fc06e12c1ea2235895e873 Mon Sep 17 00:00:00 2001 From: Theodore Brown Date: Wed, 27 Sep 2023 15:53:56 -0500 Subject: [PATCH] Optionally make the ID column writable This is needed when there is a 1:1 relationship between tables where in one table the ID column is a foreign key rather than auto-incremented. --- .github/workflows/php.yml | 4 ++-- CHANGELOG.md | 7 ++++++- composer.json | 5 ++++- src/Entities.php | 8 ++++++-- src/RouteHandler.php | 4 +++- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 07eeae8..1009b3f 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -7,7 +7,7 @@ jobs: strategy: matrix: - php: [ '7.4', '8.0', '8.1', '8.2' ] + php: [ '7.4', '8.0', '8.1', '8.2', '8.3' ] services: mysql: @@ -39,7 +39,7 @@ jobs: - name: Run Psalm run: psalm --output-format=github - if: ${{ matrix.php == '8.2' }} + if: ${{ matrix.php == '8.3' }} - name: Run PHPUnit run: vendor/bin/phpunit diff --git a/CHANGELOG.md b/CHANGELOG.md index 55c7e01..3847383 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.7.0] - 2023-09-27 +### Added +- `writableId` bool property to optionally make the ID column writable. + ## [2.6.0] - 2023-08-01 ### Added - `output` bool parameter on `Prop` constructor. @@ -142,7 +146,8 @@ return early if passed an empty IDs array. ### Changed - Initial stable release -[Unreleased]: https://github.com/theodorejb/phaster/compare/v2.6.0...HEAD +[Unreleased]: https://github.com/theodorejb/phaster/compare/v2.7.0...HEAD +[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 [2.5.0]: https://github.com/theodorejb/phaster/compare/v2.4.0...v2.5.0 [2.4.0]: https://github.com/theodorejb/phaster/compare/v2.3.0...v2.4.0 diff --git a/composer.json b/composer.json index de3ff95..4b0f577 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require-dev": { "phpunit/phpunit": "^9.6", "psalm/plugin-phpunit": "^0.18.4", - "vimeo/psalm": "^5.14" + "vimeo/psalm": "^5.15" }, "autoload": { "psr-4": {"theodorejb\\Phaster\\": "src/"} @@ -27,6 +27,9 @@ "autoload-dev": { "psr-4": {"theodorejb\\Phaster\\Test\\": "test/"} }, + "config": { + "sort-packages": true + }, "scripts": { "analyze": "psalm", "test": "phpunit" diff --git a/src/Entities.php b/src/Entities.php index 6327294..745eae1 100644 --- a/src/Entities.php +++ b/src/Entities.php @@ -23,7 +23,8 @@ abstract class Entities abstract protected function getMap(): array; protected PeachySql $db; - protected string $idField = 'id'; + public string $idField = 'id'; + protected bool $writableId = false; private string $idColumn; /** @var array */ private array $fullPropMap; @@ -56,7 +57,10 @@ public function __construct(PeachySql $db) if (isset($map[$this->idField])) { /** @psalm-suppress MixedAssignment */ $this->idColumn = $map[$this->idField]; - unset($map[$this->idField]); // prevent modifying identity column + + if (!$this->writableId) { + unset($map[$this->idField]); // prevent modifying identity column + } } else { $idParts = explode('.', $propMap[$this->idField]->col); $this->idColumn = array_pop($idParts); diff --git a/src/RouteHandler.php b/src/RouteHandler.php index 0ea9583..5b1c6c1 100644 --- a/src/RouteHandler.php +++ b/src/RouteHandler.php @@ -143,7 +143,9 @@ public function insert(string $class): callable /** @var list $data */ $body = ['ids' => $instance->addEntities($data)]; } else { - $body = ['id' => $instance->addEntities([$data])[0]]; + $ids = $instance->addEntities([$data]); + // ID isn't set when the ID column isn't auto-incremented + $body = ['id' => $ids[0] ?? $data[$instance->idField]]; } $response->getBody()->write(json_encode($body));