From 80e2fd6cc3528a3536b2792128543312ab2cf055 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 6 Oct 2015 13:23:46 +0200 Subject: [PATCH] DibiRow: shows suggestions for missing columns --- src/Dibi/Row.php | 7 +++++++ tests/dibi/Row.phpt | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Dibi/Row.php b/src/Dibi/Row.php index 6f5a0de0..636dfe98 100644 --- a/src/Dibi/Row.php +++ b/src/Dibi/Row.php @@ -47,6 +47,13 @@ class DibiRow implements ArrayAccess, IteratorAggregate, Countable } + public function __get($key) + { + $hint = DibiHelpers::getSuggestion(array_keys((array) $this), $key); + trigger_error("Attempt to read missing column '$key'" . ($hint ? ", did you mean '$hint'?" : '.'), E_USER_NOTICE); + } + + /********************* interfaces ArrayAccess, Countable & IteratorAggregate ****************d*g**/ diff --git a/tests/dibi/Row.phpt b/tests/dibi/Row.phpt index dc91ce3e..c0ac04fa 100644 --- a/tests/dibi/Row.phpt +++ b/tests/dibi/Row.phpt @@ -24,16 +24,26 @@ Assert::true(isset($row['title'])); // missing Assert::error(function () use ($row) { $x = $row->missing; -}, E_NOTICE, 'Undefined property: DibiRow::$missing'); +}, E_USER_NOTICE, "Attempt to read missing column 'missing'."); Assert::error(function () use ($row) { $x = $row['missing']; -}, E_NOTICE, 'Undefined property: DibiRow::$missing'); +}, E_USER_NOTICE, "Attempt to read missing column 'missing'."); Assert::false(isset($row->missing)); Assert::false(isset($row['missing'])); +// suggestions +Assert::error(function () use ($row) { + $x = $row->tilte; +}, E_USER_NOTICE, "Attempt to read missing column 'tilte', did you mean 'title'?"); + +Assert::error(function () use ($row) { + $x = $row['tilte']; +}, E_USER_NOTICE, "Attempt to read missing column 'tilte', did you mean 'title'?"); + + // to array Assert::same(['product_id' => 1, 'title' => 'Chair'], iterator_to_array($row)); Assert::same(['product_id' => 1, 'title' => 'Chair'], $row->toArray());