Skip to content

Commit

Permalink
Row, ActiveRow: shows suggestions for undeclared columns
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 8, 2015
1 parent 323ab1d commit e54ff96
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"php": ">=5.6.0",
"ext-pdo": "*",
"nette/caching": "~2.2",
"nette/utils": "~2.2"
"nette/utils": "~2.4"
},
"require-dev": {
"nette/tester": "~1.3",
Expand Down
3 changes: 2 additions & 1 deletion src/Database/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Row extends Nette\Utils\ArrayHash implements IRow

public function __get($key)
{
throw new Nette\MemberAccessException("Cannot read an undeclared column '$key'.");
$hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys((array) $this), $key);
throw new Nette\MemberAccessException("Cannot read an undeclared column '$key'" . ($hint ? ", did you mean '$hint'?" : '.'));
}


Expand Down
3 changes: 2 additions & 1 deletion src/Database/Table/ActiveRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ public function &__get($key)
}

$this->removeAccessColumn($key);
throw new Nette\MemberAccessException("Cannot read an undeclared column '$key'.");
$hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys($this->data), $key);
throw new Nette\MemberAccessException("Cannot read an undeclared column '$key'" . ($hint ? ", did you mean '$hint'?" : '.'));
}


Expand Down
20 changes: 18 additions & 2 deletions tests/Database/Row.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use Tester\Assert;
require __DIR__ . '/connect.inc.php'; // create $connection


test(function () use ($context) {
// numeric field
test(function () use ($context) { // numeric field
$row = $context->fetch("SELECT 123 AS {$context->getConnection()->getSupplementalDriver()->delimite('123')}, NULL as nullcol");
Assert::same(123, $row->{123});
Assert::same(123, $row->{'123'});
Expand All @@ -34,3 +33,20 @@ test(function () use ($context) {
$row[2];
}, Nette\MemberAccessException::class, "Cannot read an undeclared column '2'.");
});


test(function () use ($context) { // named field
$row = $context->fetch('SELECT 123 AS title');
Assert::same(123, $row->title);
Assert::same(123, $row[0]);
Assert::same(123, $row['title']);
Assert::false(isset($row[1])); // NULL value

Assert::error(function () use ($row) {
$row->tilte;
}, Nette\MemberAccessException::class, "Cannot read an undeclared column 'tilte', did you mean 'title'?");

Assert::error(function () use ($row) {
$row[2];
}, Nette\MemberAccessException::class, "Cannot read an undeclared column '2'.");
});
4 changes: 4 additions & 0 deletions tests/Database/Table/Table.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ test(function () use ($connection, $structure) {
$book->test;
}, Nette\MemberAccessException::class, "Cannot read an undeclared column 'test'.");

Assert::exception(function () use ($book) {
$book->tilte;
}, Nette\MemberAccessException::class, "Cannot read an undeclared column 'tilte', did you mean 'title'?");

Assert::exception(function () use ($book) {
$book->ref('test');
}, Nette\MemberAccessException::class, 'No reference found for $book->ref(test).');
Expand Down

0 comments on commit e54ff96

Please sign in to comment.