Skip to content

Commit

Permalink
Optionally make the ID column writable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
theodorejb committed Sep 27, 2023
1 parent ebfa815 commit 7404d8c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
"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/"}
},
"autoload-dev": {
"psr-4": {"theodorejb\\Phaster\\Test\\": "test/"}
},
"config": {
"sort-packages": true
},
"scripts": {
"analyze": "psalm",
"test": "phpunit"
Expand Down
8 changes: 6 additions & 2 deletions src/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Prop> */
private array $fullPropMap;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/RouteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ public function insert(string $class): callable
/** @var list<array> $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));
Expand Down

0 comments on commit 7404d8c

Please sign in to comment.