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 8b1e430
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 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
2 changes: 1 addition & 1 deletion tests/Database/Row.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test(function () use ($context) {

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

Assert::error(function () use ($row) {
$row[2];
Expand Down
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 8b1e430

Please sign in to comment.