Skip to content

Commit f8800d2

Browse files
committed
Fix equalTo array membership queries
1 parent 4b52492 commit f8800d2

2 files changed

Lines changed: 74 additions & 7 deletions

File tree

src/Parse/ParseQuery.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ public function get($objectId, $useMasterKey = false)
140140
*/
141141
public function equalTo($key, $value)
142142
{
143-
$this->addCondition($key, '$eq', $value);
143+
if (isset($this->where[$key]) && $this->isConditionMap($this->where[$key])) {
144+
$this->addCondition($key, '$eq', $value);
145+
} else {
146+
$this->where[$key] = ParseClient::_encode($value, true);
147+
}
144148

145149
return $this;
146150
}
@@ -158,10 +162,34 @@ private function addCondition($key, $condition, $value)
158162
{
159163
if (!isset($this->where[$key])) {
160164
$this->where[$key] = [];
165+
} elseif (!$this->isConditionMap($this->where[$key])) {
166+
$this->where[$key] = ['$eq' => $this->where[$key]];
161167
}
162168
$this->where[$key][$condition] = ParseClient::_encode($value, true);
163169
}
164170

171+
/**
172+
* Checks whether a where entry is an operator map rather than a direct equality value.
173+
*
174+
* @param mixed $value Where entry value.
175+
*
176+
* @return bool
177+
*/
178+
private function isConditionMap($value)
179+
{
180+
if (!is_array($value)) {
181+
return false;
182+
}
183+
184+
foreach (array_keys($value) as $key) {
185+
if (!is_string($key) || strpos($key, '$') !== 0) {
186+
return false;
187+
}
188+
}
189+
190+
return !empty($value);
191+
}
192+
165193
/**
166194
* Sets the conditions of this parse query from an array
167195
*

tests/Parse/ParseQueryTest.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,9 +2663,7 @@ public function testGetAndSetConditions()
26632663

26642664
$this->assertEquals([
26652665
'where' => [
2666-
'key' => [
2667-
'$eq' => 'value',
2668-
],
2666+
'key' => 'value',
26692667
'key2' => [
26702668
'$ne' => 'value2',
26712669
],
@@ -2726,9 +2724,7 @@ public function testCountDoesNotOverrideConditions()
27262724

27272725
$this->assertSame([
27282726
'where' => [
2729-
'country' => [
2730-
'$eq' => 'US'
2731-
]
2727+
'country' => 'US'
27322728
],
27332729
'limit' => 1,
27342730
], $query->_getOptions());
@@ -2811,4 +2807,47 @@ public function testEqualToWithSameKeyDoesNotOverrideOtherConditions()
28112807
],
28122808
], $query->_getOptions());
28132809
}
2810+
2811+
/**
2812+
* @group query-equalTo-conditions
2813+
*/
2814+
public function testEqualToAfterDirectEqualityDoesNotOverrideOtherConditions()
2815+
{
2816+
$query = new ParseQuery('TestObject');
2817+
$query->equalTo('status', 'active');
2818+
$query->notEqualTo('status', 'archived');
2819+
2820+
$this->assertSame([
2821+
'where' => [
2822+
'status' => [
2823+
'$eq' => 'active',
2824+
'$ne' => 'archived',
2825+
]
2826+
],
2827+
], $query->_getOptions());
2828+
}
2829+
2830+
/**
2831+
* @group query-equalTo-conditions
2832+
*/
2833+
public function testEqualToPointerUsesDirectEqualityForArrayFieldMembership()
2834+
{
2835+
$user = new ParseObject('_User');
2836+
$user->_mergeAfterFetch([
2837+
'objectId' => 'someUserId',
2838+
]);
2839+
2840+
$query = new ParseQuery('_Role');
2841+
$query->equalTo('users', $user);
2842+
2843+
$this->assertSame([
2844+
'where' => [
2845+
'users' => [
2846+
'__type' => 'Pointer',
2847+
'className' => '_User',
2848+
'objectId' => 'someUserId',
2849+
],
2850+
],
2851+
], $query->_getOptions());
2852+
}
28142853
}

0 commit comments

Comments
 (0)