diff --git a/src/Parse/ParseQuery.php b/src/Parse/ParseQuery.php index a8f2fd50..ad78d55c 100644 --- a/src/Parse/ParseQuery.php +++ b/src/Parse/ParseQuery.php @@ -78,6 +78,27 @@ class ParseQuery */ private $limit = -1; + /** + * The read preference for the main query. + * + * @var string + */ + private $readPreference; + + /** + * The read preference for the queries to include pointers. + * + * @var string + */ + private $includeReadPreference; + + /** + * The read preference for the sub queries. + * + * @var string + */ + private $subqueryReadPreference; + /** * Create a Parse Query for a given Parse Class. * @@ -176,6 +197,18 @@ public function _setConditions($conditions) $this->limit = $entry; break; + case 'readPreference': + $this->readPreference = $entry; + break; + + case 'includeReadPreference': + $this->includeReadPreference = $entry; + break; + + case 'subqueryReadPreference': + $this->subqueryReadPreference = $entry; + break; + // skip case 'skip': $this->skip = $entry; @@ -491,6 +524,15 @@ public function _getOptions() if ($this->count) { $opts['count'] = $this->count; } + if ($this->readPreference) { + $opts['readPreference'] = $this->readPreference; + } + if ($this->includeReadPreference) { + $opts['includeReadPreference'] = $this->includeReadPreference; + } + if ($this->subqueryReadPreference) { + $opts['subqueryReadPreference'] = $this->subqueryReadPreference; + } return $opts; } @@ -1375,4 +1417,22 @@ public function relatedTo($key, $value) return $this; } + + /** + * Changes the read preference that the backend will use when performing the query to the database. + * + * @param string $readPreference The read preference for the main query. + * @param string $includeReadPreference The read preference for the queries to include pointers. + * @param string $subqueryReadPreference The read preference for the sub queries. + * + * @return ParseQuery Returns the query, so you can chain this call. + */ + public function readPreference($readPreference, $includeReadPreference = null, $subqueryReadPreference = null) + { + $this->readPreference = $readPreference; + $this->includeReadPreference = $includeReadPreference; + $this->subqueryReadPreference = $subqueryReadPreference; + + return $this; + } } diff --git a/tests/Parse/ParseQueryTest.php b/tests/Parse/ParseQueryTest.php index bc207093..3903d8e0 100644 --- a/tests/Parse/ParseQueryTest.php +++ b/tests/Parse/ParseQueryTest.php @@ -2499,6 +2499,7 @@ public function testGetAndSetConditions() $query->notEqualTo('key2', 'value2'); $query->includeKey(['include1','include2']); $query->excludeKey(['excludeMe','excludeMeToo']); + $query->readPreference('PRIMARY', 'SECONDARY', 'SECONDARY_PREFERRED'); $query->contains('container', 'item'); $query->addDescending('desc'); $query->addAscending('asc'); @@ -2528,7 +2529,10 @@ public function testGetAndSetConditions() 'limit' => 42, 'skip' => 24, 'order' => '-desc,asc', - 'count' => 1 + 'count' => 1, + 'readPreference' => 'PRIMARY', + 'includeReadPreference' => 'SECONDARY', + 'subqueryReadPreference' => 'SECONDARY_PREFERRED', ], $conditions, 'Conditions were different than expected'); $query2 = new ParseQuery('TestObject');