Skip to content

Allow setting custom options on a query [Fixes #541] #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
*
Expand Down