Skip to content

Commit 8b1e430

Browse files
committed
Row, ActiveRow: shows suggestions for undeclared columns
1 parent 323ab1d commit 8b1e430

File tree

5 files changed

+10
-4
lines changed

5 files changed

+10
-4
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"php": ">=5.6.0",
1818
"ext-pdo": "*",
1919
"nette/caching": "~2.2",
20-
"nette/utils": "~2.2"
20+
"nette/utils": "~2.4"
2121
},
2222
"require-dev": {
2323
"nette/tester": "~1.3",

src/Database/Row.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class Row extends Nette\Utils\ArrayHash implements IRow
1818

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

2425

src/Database/Table/ActiveRow.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ public function &__get($key)
288288
}
289289

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

294295

tests/Database/Row.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test(function () use ($context) {
2828

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

3333
Assert::error(function () use ($row) {
3434
$row[2];

tests/Database/Table/Table.basic.phpt

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ test(function () use ($connection, $structure) {
8484
$book->test;
8585
}, Nette\MemberAccessException::class, "Cannot read an undeclared column 'test'.");
8686

87+
Assert::exception(function () use ($book) {
88+
$book->tilte;
89+
}, Nette\MemberAccessException::class, "Cannot read an undeclared column 'tilte', did you mean 'title'?");
90+
8791
Assert::exception(function () use ($book) {
8892
$book->ref('test');
8993
}, Nette\MemberAccessException::class, 'No reference found for $book->ref(test).');

0 commit comments

Comments
 (0)