From cffb112b37e515e9005421efbc725455288e5a96 Mon Sep 17 00:00:00 2001 From: foxbit19 Date: Thu, 9 Apr 2015 15:11:38 +0200 Subject: [PATCH] Fix null values with MongoDB distinct During the use of MongoDB driver, the *distinct* operation doesn't return the expected string key for the array of results. This kind of behaviour, is exclusively relative to MongoDB because the `$property` doesn't exists, but there is a numeric key for the array instead. The fix I propose, introduces a nullable check for `$model[$property]` and `$model->$property` and, if this check verifies the nullability, returns the first element of the array of results (*in MongoDB the distinct operation works only on 1 field*). --- src/Chumper/Datatable/Engines/BaseEngine.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Chumper/Datatable/Engines/BaseEngine.php b/src/Chumper/Datatable/Engines/BaseEngine.php index b2554f1..5b7238c 100644 --- a/src/Chumper/Datatable/Engines/BaseEngine.php +++ b/src/Chumper/Datatable/Engines/BaseEngine.php @@ -218,7 +218,21 @@ public function showColumns($cols) else { $this->columns->put($property, new FunctionColumn($property, function($model) use($property){ - try{return is_array($model)?$model[$property]:$model->$property;}catch(Exception $e){return null;} + try + { + if (is_array($model)) + { + return $model[$property] == null ? $model[0] : $model[$property]; + } + else + { + return $model->$property == null ? $model[0] : $model->$property; + } + } + catch(Exception $e) + { + return null; + } })); } $this->showColumns[] = $property;