Skip to content

Commit 66983b4

Browse files
miranovydg
authored andcommitted
ActiveRow: __isset checks the existence of the relation (#196)
1 parent f4d7fc0 commit 66983b4

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/Database/Table/ActiveRow.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ public function __isset($key)
287287
if ($this->accessColumn($key)) {
288288
return isset($this->data[$key]);
289289
}
290+
291+
$referenced = $this->table->getReferencedTable($this, $key);
292+
if ($referenced !== false) {
293+
$this->accessColumn($key, false);
294+
return (bool) $referenced;
295+
}
296+
290297
$this->removeAccessColumn($key);
291298
return false;
292299
}

tests/Database/Table/Table.discoveredReflection.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ test(function () use ($context) {
8080
Assert::true(empty($book->translator_id));
8181
Assert::false(isset($book->test));
8282

83-
Assert::false(isset($book->author));
83+
Assert::true(isset($book->author));
8484
Assert::false(isset($book->translator));
85-
Assert::true(empty($book->author));
85+
Assert::false(empty($book->author));
8686
Assert::true(empty($book->translator));
8787
});
8888

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* @dataProvider? ../databases.ini
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Tester\Assert;
10+
11+
require __DIR__ . '/../../connect.inc.php'; // create $connection
12+
13+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../../files/{$driverName}-nette_test1.sql");
14+
15+
16+
test(function () use ($context) {
17+
$row = $context->table('book')->get(2);
18+
Assert::same('Jakub Vrana', $row->author->name);
19+
Assert::true(isset($row->author->name));
20+
});

0 commit comments

Comments
 (0)