Skip to content

Commit

Permalink
Fix bug with eager loading
Browse files Browse the repository at this point in the history
Bug occured when accessing relations on a relation that was being iterated over
  • Loading branch information
solleer committed Aug 16, 2020
1 parent 72aaf1c commit 8a2b22a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Maphper/Relation/One.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ private function loadDataIntoSiblings($results) {
}

foreach ($this->siblings as $sibling) {
if (isset($cache[$sibling->parentObject->{$this->parentField}])) $sibling->data = $cache[$sibling->parentObject->{$this->parentField}];
if ($sibling->parentField === $this->parentField &&
isset($cache[$sibling->parentObject->{$this->parentField}]))$sibling->data = $cache[$sibling->parentObject->{$this->parentField}];
}
/*
foreach ($this->siblings as $sibling) {
Expand Down
33 changes: 33 additions & 0 deletions tests/MySqlDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,39 @@ public function testPkAccessWithFilter() {
$this->assertFalse(isset($mapper[3]->id));
}

public function testOneRelationsDuringIteration() {
$this->dropTable('site');
$this->dropTable('blog');
$this->dropTable('author');
$this->dropTable('content');

$sites = new \Maphper\Maphper($this->getDataSource('site', 'id', ['editmode' => true]));
$blogs = new \Maphper\Maphper($this->getDataSource('blog', 'id', ['editmode' => true]));
$authors = new \Maphper\Maphper($this->getDataSource('author', 'id', ['editmode' => true]));
$content = new \Maphper\Maphper($this->getDataSource('content', 'id', ['editmode' => true]));

$blogs->addRelation('author', new \Maphper\Relation\One($authors, 'authorId', 'id'));
$blogs->addRelation('content', new \Maphper\Relation\One($content, 'cId', 'id'));
$sites->addRelation('blogs', new \Maphper\Relation\Many($blogs, 'id', 'sId'));

$sites[1] = (object)[];
$blogs[1] = (object)[
'sId' => 1,
'authorId' => 1,
'cId' => 1,
];
$authors[1] = (object)[
'name' => 'Tester',
];
$content[1] = (object)[
'text' => 'Filler',
];

$iter = $sites[1]->blogs->getIterator();
$currentBlog = $iter->current();
$this->assertEquals('Tester', $currentBlog->author->name);
$this->assertEquals('Filler', $currentBlog->content->text);
}

public function testInsertNoEditModeNoColumn() {

Expand Down

0 comments on commit 8a2b22a

Please sign in to comment.