Skip to content

Commit b1b0b5c

Browse files
committed
fix: restore direct equalTo pointer queries
1 parent 4b52492 commit b1b0b5c

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

src/Parse/ParseQuery.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,39 @@ public function get($objectId, $useMasterKey = false)
140140
*/
141141
public function equalTo($key, $value)
142142
{
143+
if ($value instanceof ParseObject && !isset($this->where[$key])) {
144+
$this->where[$key] = ParseClient::_encode($value, true);
145+
146+
return $this;
147+
}
148+
143149
$this->addCondition($key, '$eq', $value);
144150

145151
return $this;
146152
}
147153

154+
/**
155+
* Checks whether a where entry is a map of query operators.
156+
*
157+
* @param mixed $entry The where entry to check
158+
*
159+
* @return bool
160+
*/
161+
private function isConditionMap($entry)
162+
{
163+
if (!is_array($entry)) {
164+
return false;
165+
}
166+
167+
foreach (array_keys($entry) as $key) {
168+
if (is_string($key) && strlen($key) > 0 && $key[0] === '$') {
169+
return true;
170+
}
171+
}
172+
173+
return false;
174+
}
175+
148176
/**
149177
* Helper for condition queries.
150178
*
@@ -156,9 +184,15 @@ public function equalTo($key, $value)
156184
*/
157185
private function addCondition($key, $condition, $value)
158186
{
187+
$hasExistingCondition = isset($this->where[$key]);
159188
if (!isset($this->where[$key])) {
160189
$this->where[$key] = [];
161190
}
191+
if ($hasExistingCondition && !$this->isConditionMap($this->where[$key])) {
192+
$this->where[$key] = [
193+
'$eq' => $this->where[$key],
194+
];
195+
}
162196
$this->where[$key][$condition] = ParseClient::_encode($value, true);
163197
}
164198

tests/Parse/ParseQueryTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,4 +2811,55 @@ public function testEqualToWithSameKeyDoesNotOverrideOtherConditions()
28112811
],
28122812
], $query->_getOptions());
28132813
}
2814+
2815+
/**
2816+
* @group query-equalTo-conditions
2817+
*/
2818+
public function testEqualToObjectUsesDirectPointerCondition()
2819+
{
2820+
$user = ParseObject::create('_User', 'someUserId');
2821+
2822+
$query = new ParseQuery('_Role');
2823+
$query->equalTo('users', $user);
2824+
2825+
$this->assertSame([
2826+
'where' => [
2827+
'users' => [
2828+
'__type' => 'Pointer',
2829+
'className' => '_User',
2830+
'objectId' => 'someUserId',
2831+
],
2832+
],
2833+
], $query->_getOptions());
2834+
}
2835+
2836+
/**
2837+
* @group query-equalTo-conditions
2838+
*/
2839+
public function testObjectEqualToWithSameKeyPreservesOtherConditions()
2840+
{
2841+
$user = ParseObject::create('_User', 'someUserId');
2842+
$otherUser = ParseObject::create('_User', 'otherUserId');
2843+
2844+
$query = new ParseQuery('_Role');
2845+
$query->equalTo('users', $user);
2846+
$query->notEqualTo('users', $otherUser);
2847+
2848+
$this->assertSame([
2849+
'where' => [
2850+
'users' => [
2851+
'$eq' => [
2852+
'__type' => 'Pointer',
2853+
'className' => '_User',
2854+
'objectId' => 'someUserId',
2855+
],
2856+
'$ne' => [
2857+
'__type' => 'Pointer',
2858+
'className' => '_User',
2859+
'objectId' => 'otherUserId',
2860+
],
2861+
],
2862+
],
2863+
], $query->_getOptions());
2864+
}
28142865
}

0 commit comments

Comments
 (0)