Skip to content

Commit 672b48e

Browse files
committed
Add afterInsert hook
1 parent 2a57298 commit 672b48e

4 files changed

Lines changed: 35 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ 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+
## [Unreleased]
8+
### Added
9+
- `afterInsert()` hook which runs after rows are inserted, before their IDs are returned.
10+
It receives the inserted rows and their generated IDs, enabling follow-up work
11+
(e.g. sending notifications) that depends on a successful insert.
12+
713
## [4.0.0] - 2025-01-03
814
### Changed
915
- `getMap()` is no longer abstract and returns an empty array by default,

src/Entities.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ protected function processRow(array $row, array $ids): array
141141
return $row;
142142
}
143143

144+
/**
145+
* Runs after rows are inserted, before their IDs are returned.
146+
* The arrays are parallel: $ids[$i] is the generated ID of $rows[$i].
147+
* @param list<int> $ids
148+
* @param list<array<string, mixed>> $rows the inserted column/value rows
149+
*/
150+
protected function afterInsert(array $ids, array $rows): void {}
151+
144152
/**
145153
* @param list<string|int> $ids
146154
*/
@@ -214,9 +222,12 @@ public function addEntities(array $entities): array
214222
}
215223

216224
$ids = $this->db->insertRows($this->getTableName(), $rows, $this->getIdentityIncrement())->ids;
225+
$this->afterInsert($ids, $rows); // $ids is parallel to $rows before existing IDs are merged in
226+
217227
foreach ($existingIds as $offset => $id) {
218228
array_splice($ids, $offset, 0, [$id]);
219229
}
230+
220231
return $ids;
221232
}
222233

tests/DbTestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,16 @@ public function testModernUsers(): void
320320
$ids = $entities->addEntities($users);
321321
$this->assertSame(-42, $ids[1]); // manually set ID in processValues
322322

323+
// afterInsert receives only the inserted rows, with IDs parallel to them (excluding the
324+
// manually set existing ID at index 1)
325+
$insertedIds = $ids;
326+
array_splice($insertedIds, 1, 1);
327+
$this->assertNotNull($entities->afterInsertResult);
328+
$this->assertSame($insertedIds, $entities->afterInsertResult['ids']);
329+
$this->assertCount(9, $entities->afterInsertResult['rows']);
330+
$this->assertSame('Modern user 1', $entities->afterInsertResult['rows'][0]['name']);
331+
$this->assertSame('Modern user 3 modified', $entities->afterInsertResult['rows'][1]['name']);
332+
323333
$db->insertRow('UserThings', ['user_id' => $ids[3]]);
324334

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

tests/src/ModernUsers.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class ModernUsers extends Entities
88
{
9+
/** @var array{ids: list<int>, rows: list<array<string, mixed>>}|null */
10+
public ?array $afterInsertResult = null;
11+
912
protected function getTableName(): string
1013
{
1114
return 'Users';
@@ -63,4 +66,9 @@ protected function processValues(array $data, array $ids): array
6366

6467
return $data;
6568
}
69+
70+
protected function afterInsert(array $ids, array $rows): void
71+
{
72+
$this->afterInsertResult = ['ids' => $ids, 'rows' => $rows];
73+
}
6674
}

0 commit comments

Comments
 (0)