Skip to content
Open
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions src/Parse/ParseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,39 @@ public function get($objectId, $useMasterKey = false)
*/
public function equalTo($key, $value)
{
if ($value instanceof ParseObject && !isset($this->where[$key])) {
$this->where[$key] = ParseClient::_encode($value, true);

return $this;
}

$this->addCondition($key, '$eq', $value);

return $this;
}

/**
* Checks whether a where entry is a map of query operators.
*
* @param mixed $entry The where entry to check
*
* @return bool
*/
private function isConditionMap($entry)
{
if (!is_array($entry)) {
return false;
}

foreach (array_keys($entry) as $key) {
if (is_string($key) && strlen($key) > 0 && $key[0] === '$') {
return true;
}
}

return false;
}

/**
* Helper for condition queries.
*
Expand All @@ -156,9 +184,15 @@ public function equalTo($key, $value)
*/
private function addCondition($key, $condition, $value)
{
$hasExistingCondition = isset($this->where[$key]);
if (!isset($this->where[$key])) {
$this->where[$key] = [];
}
if ($hasExistingCondition && !$this->isConditionMap($this->where[$key])) {
$this->where[$key] = [
'$eq' => $this->where[$key],
];
}
$this->where[$key][$condition] = ParseClient::_encode($value, true);
}

Expand Down
51 changes: 51 additions & 0 deletions tests/Parse/ParseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2811,4 +2811,55 @@ public function testEqualToWithSameKeyDoesNotOverrideOtherConditions()
],
], $query->_getOptions());
}

/**
* @group query-equalTo-conditions
*/
public function testEqualToObjectUsesDirectPointerCondition()
{
$user = ParseObject::create('_User', 'someUserId');

$query = new ParseQuery('_Role');
$query->equalTo('users', $user);

$this->assertSame([
'where' => [
'users' => [
'__type' => 'Pointer',
'className' => '_User',
'objectId' => 'someUserId',
],
],
], $query->_getOptions());
}

/**
* @group query-equalTo-conditions
*/
public function testObjectEqualToWithSameKeyPreservesOtherConditions()
{
$user = ParseObject::create('_User', 'someUserId');
$otherUser = ParseObject::create('_User', 'otherUserId');

$query = new ParseQuery('_Role');
$query->equalTo('users', $user);
$query->notEqualTo('users', $otherUser);

$this->assertSame([
'where' => [
'users' => [
'$eq' => [
'__type' => 'Pointer',
'className' => '_User',
'objectId' => 'someUserId',
],
'$ne' => [
'__type' => 'Pointer',
'className' => '_User',
'objectId' => 'otherUserId',
],
],
],
], $query->_getOptions());
}
}