Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 4 additions & 28 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use SplObjectStorage;
use Throwable;

use function array_key_exists;
use function array_keys;
use function array_merge;
use function array_push;
Expand Down Expand Up @@ -287,33 +286,10 @@ private function generateQuery(Collection $collection): Sql
/** @return array<string, mixed> */
private function extractColumns(object $entity, Collection $collection): array
{
$primaryName = $this->style->identifier($collection->name);
$cols = $this->entityFactory->extractProperties($entity);

foreach ($cols as $key => $c) {
if (is_object($c) && $this->style->isRelationProperty($key)) {
unset($cols[$key]);

continue;
}

if (is_object($c)) {
$cols[$key] = $this->entityFactory->get($c, $primaryName);

continue;
}

if (
!$this->style->isRelationProperty($key)
|| !array_key_exists($this->style->remoteIdentifier($key), $cols)
) {
continue;
}

unset($cols[$key]);
}

return $this->filterColumns($cols, $collection);
return $this->filterColumns(
$this->entityFactory->extractColumns($entity),
$collection,
);
}

/** @param array<string, Collection> $collections */
Expand Down
11 changes: 5 additions & 6 deletions tests/Hydrators/FlatNumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Respect\Data\Collections\Collection;
use Respect\Data\Collections\Typed;
use Respect\Data\EntityFactory;
use stdClass;
use Respect\Relational\Bug;

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

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

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

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

$this->assertNotFalse($result);
$result->rewind();
$this->assertInstanceOf(stdClass::class, $result->current());
$this->assertInstanceOf(Bug::class, $result->current());
}
}
Loading
Loading