From 19a71838303e983ccfaa0f86439c81fea06bf72d Mon Sep 17 00:00:00 2001 From: Ryan Hayle Date: Mon, 19 Sep 2016 13:00:37 -0500 Subject: [PATCH] Allow setting custom options on a query [Fixes #541] --- src/Jenssegers/Mongodb/Query/Builder.php | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 74b9ee3a6..9e0dad023 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -41,6 +41,13 @@ class Builder extends BaseBuilder */ public $hint; + /** + * Custom options to add to the query. + * + * @var array + */ + public $options = []; + /** * Indicate if we are executing a pagination query. * @@ -263,6 +270,11 @@ public function getFresh($columns = []) 'typeMap' => ['root' => 'array', 'document' => 'array'], ]; + // Add custom query options + if (count($this->options)) { + $options = array_merge($options, $this->options); + } + // Execute aggregation $results = iterator_to_array($this->collection->aggregate($pipeline, $options)); @@ -321,6 +333,11 @@ public function getFresh($columns = []) // Fix for legacy support, converts the results to arrays instead of objects. $options['typeMap'] = ['root' => 'array', 'document' => 'array']; + // Add custom query options + if (count($this->options)) { + $options = array_merge($options, $this->options); + } + // Execute query and get MongoCursor $cursor = $this->collection->find($wheres, $options); @@ -1019,6 +1036,19 @@ protected function compileWhereRaw($where) return $where['sql']; } + /** + * Set custom options for the query. + * + * @param array $options + * @return $this + */ + public function options(array $options) + { + $this->options = $options; + + return $this; + } + /** * Handle dynamic method calls into the method. *