Skip to content

Commit 4bf9ca9

Browse files
author
Ryan Hayle
committed
Allow setting custom options on a query [Fixes mongodb#541]
1 parent f829fb0 commit 4bf9ca9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class Builder extends BaseBuilder
4141
*/
4242
public $hint;
4343

44+
/**
45+
* Custom options to add to the query.
46+
*
47+
* @var array
48+
*/
49+
public $options = [];
50+
4451
/**
4552
* Indicate if we are executing a pagination query.
4653
*
@@ -255,6 +262,11 @@ public function getFresh($columns = [])
255262
'typeMap' => ['root' => 'array', 'document' => 'array'],
256263
];
257264

265+
// Add custom query options
266+
if (count($this->options)) {
267+
$options = array_merge($options, $this->options);
268+
}
269+
258270
// Execute aggregation
259271
$results = iterator_to_array($this->collection->aggregate($pipeline, $options));
260272

@@ -313,6 +325,11 @@ public function getFresh($columns = [])
313325
// Fix for legacy support, converts the results to arrays instead of objects.
314326
$options['typeMap'] = ['root' => 'array', 'document' => 'array'];
315327

328+
// Add custom query options
329+
if (count($this->options)) {
330+
$options = array_merge($options, $this->options);
331+
}
332+
316333
// Execute query and get MongoCursor
317334
$cursor = $this->collection->find($wheres, $options);
318335

@@ -1020,6 +1037,19 @@ protected function compileWhereRaw($where)
10201037
return $where['sql'];
10211038
}
10221039

1040+
/**
1041+
* Set custom options for the query.
1042+
*
1043+
* @param array $options
1044+
* @return $this
1045+
*/
1046+
public function options(array $options)
1047+
{
1048+
$this->options = $options;
1049+
1050+
return $this;
1051+
}
1052+
10231053
/**
10241054
* Handle dynamic method calls into the method.
10251055
*

0 commit comments

Comments
 (0)