From b1b0b5c44bdb3e75bc5aee546531cf69a4d727b4 Mon Sep 17 00:00:00 2001 From: Shailesh Rana Date: Mon, 11 May 2026 04:48:35 +0530 Subject: [PATCH] fix: restore direct equalTo pointer queries --- src/Parse/ParseQuery.php | 34 +++++++++++++++++++++++ tests/Parse/ParseQueryTest.php | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/Parse/ParseQuery.php b/src/Parse/ParseQuery.php index be7826b8..e6c04d42 100644 --- a/src/Parse/ParseQuery.php +++ b/src/Parse/ParseQuery.php @@ -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. * @@ -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); } diff --git a/tests/Parse/ParseQueryTest.php b/tests/Parse/ParseQueryTest.php index ae251ab1..dad4f7c5 100644 --- a/tests/Parse/ParseQueryTest.php +++ b/tests/Parse/ParseQueryTest.php @@ -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()); + } }