Skip to content

Commit c2dec9a

Browse files
committed
Make writable map optional, and remove special rules for ID column
1 parent 267d4c0 commit c2dec9a

File tree

5 files changed

+45
-34
lines changed

5 files changed

+45
-34
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [4.0.0] - 2025-01-03
8+
### Changed
9+
- `getMap()` is no longer abstract and returns an empty array by default,
10+
so it is no longer necessary to implement for read-only APIs.
11+
- If the ID column is returned from `getMap()`, it is now automatically writable
12+
without having to set a `writableId` flag.
13+
- `getSelectMap()` now returns an empty array by default (rather than the `getMap()` value),
14+
so it is no longer necessary to implement this method just to prevent selecting write-only
15+
properties when using `getSelectProps()`.
16+
17+
> [!Important]
18+
> When upgrading, remove readonly ID properties from `getMap()`, and implement
19+
> `getSelectMap()` if any writable property is not returned by `getSelectProps()`.
20+
21+
### Removed
22+
- Unnecessary `getDefaultValues()` method. Defaults can be set via `processValues()` instead.
23+
- `writableId` bool property.
24+
725
## [3.0.0] - 2024-10-29
826
### Added
927
- Official support for PostgreSQL.
@@ -187,6 +205,7 @@ return early if passed an empty IDs array.
187205
- Initial stable release
188206

189207

208+
[4.0.0]: https://github.com/devtheorem/phaster/compare/v3.0.0...v4.0.0
190209
[3.0.0]: https://github.com/devtheorem/phaster/compare/v2.9.0...v3.0.0
191210
[2.9.0]: https://github.com/devtheorem/phaster/compare/v2.8.0...v2.9.0
192211
[2.8.0]: https://github.com/devtheorem/phaster/compare/v2.7.0...v2.8.0

src/Entities.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@
88

99
abstract class Entities
1010
{
11-
/**
12-
* Returns a map of properties to columns for the current table
13-
* @return array<string, mixed>
14-
*/
15-
abstract protected function getMap(): array;
16-
1711
protected PeachySql $db;
1812
public string $idField = 'id';
19-
protected bool $writableId = false;
2013
private string $idColumn;
2114
/** @var array<string, Prop> */
2215
private array $fullPropMap;
@@ -37,25 +30,13 @@ public function __construct(PeachySql $db)
3730
$propMap = Helpers::propListToPropMap([...array_values($bcProps), ...$selectProps]);
3831

3932
if (!isset($propMap[$this->idField])) {
40-
throw new \Exception('Missing required id property in map');
41-
}
42-
43-
$map = $this->getMap();
44-
45-
if (isset($map[$this->idField])) {
46-
/** @psalm-suppress MixedAssignment */
47-
$this->idColumn = $map[$this->idField];
48-
49-
if (!$this->writableId) {
50-
unset($map[$this->idField]); // prevent modifying identity column
51-
}
52-
} else {
53-
$idParts = explode('.', $propMap[$this->idField]->col);
54-
$this->idColumn = array_pop($idParts);
33+
throw new \Exception("Missing required {$this->idField} property in select map");
5534
}
5635

36+
$idParts = explode('.', $propMap[$this->idField]->col);
37+
$this->idColumn = array_pop($idParts);
5738
$this->fullPropMap = $propMap;
58-
$this->map = $map;
39+
$this->map = $this->getMap();
5940
}
6041

6142
/**
@@ -66,6 +47,15 @@ protected function getTableName(): string
6647
return (new \ReflectionClass($this))->getShortName();
6748
}
6849

50+
/**
51+
* Returns a map of properties to writable columns for the current table.
52+
* @return array<string, mixed>
53+
*/
54+
protected function getMap(): array
55+
{
56+
return [];
57+
}
58+
6959
/**
7060
* Returns the identity increment value of the table
7161
*/
@@ -99,12 +89,12 @@ protected function getDefaultSort(): array
9989
}
10090

10191
/**
102-
* Can be used to return a separate property map for filtering/sorting (but not inserting/updating)
92+
* Returns a map of properties to columns for selecting/filtering/sorting.
10393
* @return array<string, mixed>
10494
*/
10595
protected function getSelectMap(): array
10696
{
107-
return $this->getMap();
97+
return [];
10898
}
10999

110100
/**

test/DbTestCase.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,14 @@ public function testModernUsers(): void
322322

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

325-
$actual = $entities->getEntitiesByIds([$ids[2], $ids[3]], ['id', 'name', 'isDisabled', 'computed', 'weight', 'thing.uid']);
325+
$actual = $entities->getEntitiesByIds([$ids[2], $ids[3]], ['id', 'name', 'isDisabled', 'computed', 'thing.uid']);
326326

327327
$expected = [
328328
[
329329
'id' => $ids[3],
330330
'name' => 'Modern user 4',
331331
'isDisabled' => false,
332332
'computed' => 41.0,
333-
'weight' => $db->options->floatSelectedAsString ? '40' : 40.0,
334333
'thing' => [
335334
'uid' => $ids[3],
336335
],
@@ -340,7 +339,6 @@ public function testModernUsers(): void
340339
'name' => 'Modern user 3 modified',
341340
'isDisabled' => false,
342341
'computed' => 31.0,
343-
'weight' => $db->options->floatSelectedAsString ? '30' : 30.0,
344342
'thing' => null,
345343
],
346344
];

test/src/ModernUsers.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ protected function getTableName(): string
1414
protected function getMap(): array
1515
{
1616
return [
17-
'id' => 'user_id',
1817
'name' => 'name',
1918
'birthday' => 'dob',
2019
'weight' => 'weight',
@@ -31,11 +30,12 @@ protected function getSelectProps(): array
3130

3231
return [
3332
new Prop('id', 'u.user_id'),
34-
new Prop('name', 'name', false, true, 'username'),
35-
new Prop('isDisabled', 'is_disabled', false, true, '', 'bool'),
36-
new Prop('computed', '', false, true, '', null, false, $getValue, ['weight']),
33+
new Prop('name', 'name', alias: 'username'),
34+
new Prop('weight', 'weight'),
35+
new Prop('isDisabled', 'is_disabled', type: 'bool'),
36+
new Prop('computed', getValue: $getValue, dependsOn: ['weight']),
3737
new Prop('thing.id', 'ut.thing_id', true),
38-
new Prop('thing.uid', 'ut.user_id', false, true, 'thing_user'),
38+
new Prop('thing.uid', 'ut.user_id', alias: 'thing_user'),
3939
];
4040
}
4141

test/src/Users.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ class Users extends Entities
1010
protected function getMap(): array
1111
{
1212
return [
13-
'id' => 'user_id',
1413
'name' => 'name',
1514
'birthday' => 'dob',
1615
'weight' => 'weight',
1716
'isDisabled' => 'is_disabled',
1817
];
1918
}
2019

20+
protected function getSelectMap(): array
21+
{
22+
return ['id' => 'user_id', ...$this->getMap()];
23+
}
24+
2125
protected function getSelectProps(): array
2226
{
2327
return [

0 commit comments

Comments
 (0)