diff --git a/src/Jenssegers/Mongodb/Builder.php b/src/Jenssegers/Mongodb/Builder.php index 5f641bc83..62780a052 100644 --- a/src/Jenssegers/Mongodb/Builder.php +++ b/src/Jenssegers/Mongodb/Builder.php @@ -5,6 +5,7 @@ use MongoDate; use DateTime; use Closure; +use Illuminate\Database\Query\Processors\Processor; class Builder extends \Illuminate\Database\Query\Builder { @@ -15,6 +16,13 @@ class Builder extends \Illuminate\Database\Query\Builder { */ protected $collection; + /** + * The database query post processor instance. + * + * @var \Illuminate\Database\Query\Processors\Processor + */ + protected $processor; + /** * All of the available operators. * @@ -36,9 +44,10 @@ class Builder extends \Illuminate\Database\Query\Builder { * @param Connection $connection * @return void */ - public function __construct(Connection $connection) + public function __construct(Connection $connection, Processor $processor = null) { $this->connection = $connection; + $this->processor = $processor; } /** @@ -147,8 +156,15 @@ public function getFresh($columns = array('*')) $this->from . '.aggregate(' . json_encode($pipeline) . ')', array(), $this->connection->getElapsedTime($start)); + //Post process the data + $result = $results['result']; + if($this->processor !== null) + { + $result = $this->processor->processSelect($this, $result); + } + // Return results - return $results['result']; + return $result; } // Distinct query @@ -165,6 +181,12 @@ public function getFresh($columns = array('*')) $this->from . '.distinct("' . $column . '", ' . json_encode($wheres) . ')', array(), $this->connection->getElapsedTime($start)); + //Post process the data + if($this->processor !== null) + { + $result = $this->processor->processSelect($this, $result); + } + return $result; } @@ -190,8 +212,15 @@ public function getFresh($columns = array('*')) $this->from . '.find(' . json_encode($wheres) . ', ' . json_encode($columns) . ')', array(), $this->connection->getElapsedTime($start)); + //Post process the data + $result = iterator_to_array($cursor, false); + if($this->processor !== null) + { + $result = $this->processor->processSelect($this, $result); + } + // Return results as an array with numeric keys - return iterator_to_array($cursor, false); + return $result; } } diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 3b0293ae3..bf9bb6382 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -1,6 +1,7 @@ db = $this->connection->{$config['database']}; + + // If the ConvertApplicableObjects options is set, use the convertToObjectsPostProcessor + if(array_key_exists('convertToObjectsPostProcessor', $config) && $config['convertToObjectsPostProcessor'] == true) + { + $this->setPostProcessor(new ConvertToObjectsPostProcessor()); + } } /** @@ -50,7 +57,7 @@ public function __construct(array $config) */ public function collection($collection) { - $query = new QueryBuilder($this); + $query = new QueryBuilder($this, $this->getPostProcessor()); return $query->from($collection); } diff --git a/src/Jenssegers/Mongodb/Model.php b/src/Jenssegers/Mongodb/Model.php index 533935a36..ecf57775e 100644 --- a/src/Jenssegers/Mongodb/Model.php +++ b/src/Jenssegers/Mongodb/Model.php @@ -245,7 +245,8 @@ public function belongsToMany($related, $collection = null, $foreignKey = null, */ protected function newBaseQueryBuilder() { - return new QueryBuilder($this->getConnection()); + $conn = $this->getConnection(); + return new QueryBuilder($conn, $conn->getPostProcessor()); } /** diff --git a/src/Jenssegers/Mongodb/Query/Processors/ConvertToObjectsPostProcessor.php b/src/Jenssegers/Mongodb/Query/Processors/ConvertToObjectsPostProcessor.php new file mode 100644 index 000000000..22c64c969 --- /dev/null +++ b/src/Jenssegers/Mongodb/Query/Processors/ConvertToObjectsPostProcessor.php @@ -0,0 +1,55 @@ +recursiveTransform($resultingItem); + } + + //Return the transformed results + return $results; + } + + public function recursiveTransform(&$data) + { + + //If the data is an array and doesn't contains only numeric keys + //Filter out keys that are not numeric, if none, abort, they are a collection of sub items, not a sub document + if(is_array($data) && count(array_filter(array_keys($data), function($item){ + return !is_numeric($item); + })) > 0) + { + + //Recursively process the sub items + foreach($data as &$subdata) + { + $this->recursiveTransform($subdata); + } + + //Now that it is processed, convert it + $data = (Object)$data; + + } + } + +} \ No newline at end of file