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..66ef558 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]); + // array is empty when the ID column isn't auto-incremented + $body = ['id' => $ids[0] ?? $data[$instance->idField]]; } $response->getBody()->write(json_encode($body));