Skip to content

Commit f0f0ba4

Browse files
committed
Remove stdClass support, enable pure entity trees
Drop stdClass entities: all hydration now produces typed classes. Simplify extractColumns to a one-liner delegating to EntityFactory::extractColumns() + filterColumns(). Simplify getRelatedEntity keeping the FK-field fallback for backward compatibility with OtherEntity-style setters. Entity stubs use pure entity trees: relation properties ($post, $author) replace FK scalars. OtherEntity\Post marks its derived $author as #[NotPersistable] since it keeps explicit $author_id. Remove 5 redundant tests (identical duplicate + fetch/fetchAll pairs). Add coverage for LEFT JOIN null-child, non-existent row fetch, pure entity tree FK derivation on persist.
1 parent 325dea6 commit f0f0ba4

11 files changed

Lines changed: 308 additions & 381 deletions

File tree

src/Mapper.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use SplObjectStorage;
1919
use Throwable;
2020

21-
use function array_key_exists;
2221
use function array_keys;
2322
use function array_merge;
2423
use function array_push;
@@ -287,33 +286,10 @@ private function generateQuery(Collection $collection): Sql
287286
/** @return array<string, mixed> */
288287
private function extractColumns(object $entity, Collection $collection): array
289288
{
290-
$primaryName = $this->style->identifier($collection->name);
291-
$cols = $this->entityFactory->extractProperties($entity);
292-
293-
foreach ($cols as $key => $c) {
294-
if (is_object($c) && $this->style->isRelationProperty($key)) {
295-
unset($cols[$key]);
296-
297-
continue;
298-
}
299-
300-
if (is_object($c)) {
301-
$cols[$key] = $this->entityFactory->get($c, $primaryName);
302-
303-
continue;
304-
}
305-
306-
if (
307-
!$this->style->isRelationProperty($key)
308-
|| !array_key_exists($this->style->remoteIdentifier($key), $cols)
309-
) {
310-
continue;
311-
}
312-
313-
unset($cols[$key]);
314-
}
315-
316-
return $this->filterColumns($cols, $collection);
289+
return $this->filterColumns(
290+
$this->entityFactory->extractColumns($entity),
291+
$collection,
292+
);
317293
}
318294

319295
/** @param array<string, Collection> $collections */

tests/Hydrators/FlatNumTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Respect\Data\Collections\Collection;
1212
use Respect\Data\Collections\Typed;
1313
use Respect\Data\EntityFactory;
14-
use stdClass;
14+
use Respect\Relational\Bug;
1515

1616
#[CoversClass(FlatNum::class)]
1717
class FlatNumTest extends TestCase
@@ -28,7 +28,7 @@ protected function setUp(): void
2828
$this->pdo->exec('CREATE TABLE post (id INTEGER PRIMARY KEY, title TEXT, author_id INTEGER)');
2929
$this->pdo->exec("INSERT INTO author VALUES (1, 'Alice')");
3030
$this->pdo->exec("INSERT INTO post VALUES (10, 'Hello', 1)");
31-
$this->factory = new EntityFactory();
31+
$this->factory = new EntityFactory(entityNamespace: 'Respect\\Relational\\');
3232
}
3333

3434
#[Test]
@@ -83,19 +83,18 @@ public function hydrateReturnsFalseForEmptyResult(): void
8383
public function hydrateResolvesTypedEntity(): void
8484
{
8585
$this->pdo->exec('CREATE TABLE issue (id INTEGER PRIMARY KEY, title TEXT, type TEXT)');
86-
$this->pdo->exec("INSERT INTO issue VALUES (1, 'Bug Report', 'stdClass')");
86+
$this->pdo->exec("INSERT INTO issue VALUES (1, 'Bug Report', 'Bug')");
8787

8888
$stmt = $this->pdo->prepare('SELECT id, title, type FROM issue');
8989
$stmt->execute();
9090
$row = $stmt->fetch(PDO::FETCH_NUM);
9191

92-
$factory = new EntityFactory(entityNamespace: 'Respect\Relational\Hydrators\\');
9392
$hydrator = new FlatNum($stmt);
9493
$collection = Typed::issue('type');
95-
$result = $hydrator->hydrate($row, $collection, $factory);
94+
$result = $hydrator->hydrate($row, $collection, $this->factory);
9695

9796
$this->assertNotFalse($result);
9897
$result->rewind();
99-
$this->assertInstanceOf(stdClass::class, $result->current());
98+
$this->assertInstanceOf(Bug::class, $result->current());
10099
}
101100
}

0 commit comments

Comments
 (0)