diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index a95c4cc7e..1f11aed4b 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1651,6 +1651,8 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, $where = []; $orders = []; + $queries = array_map(fn ($query) => clone $query, $queries); + $orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) { '$id' => '_uid', '$internalId' => '_id', @@ -1852,6 +1854,8 @@ public function count(string $collection, array $queries = [], ?int $max = null) $where = []; $limit = \is_null($max) ? '' : 'LIMIT :max'; + $queries = array_map(fn ($query) => clone $query, $queries); + $conditions = $this->getSQLConditions($queries); if(!empty($conditions)) { $where[] = $conditions; @@ -1923,6 +1927,8 @@ public function sum(string $collection, string $attribute, array $queries = [], $where = []; $limit = \is_null($max) ? '' : 'LIMIT :max'; + $queries = array_map(fn ($query) => clone $query, $queries); + foreach ($queries as $query) { $where[] = $this->getSQLCondition($query); } diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index f8a84fbe8..43565ca29 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -951,6 +951,7 @@ public function updateAttribute(string $collection, string $id, string $type, in public function find(string $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER): array { $name = $this->getNamespace() . '_' . $this->filter($collection); + $queries = array_map(fn ($query) => clone $query, $queries); $filters = $this->buildFilters($queries); @@ -1212,6 +1213,8 @@ public function count(string $collection, array $queries = [], ?int $max = null) { $name = $this->getNamespace() . '_' . $this->filter($collection); + $queries = array_map(fn ($query) => clone $query, $queries); + $filters = []; $options = []; @@ -1252,6 +1255,7 @@ public function sum(string $collection, string $attribute, array $queries = [], $name = $this->getNamespace() . '_' . $this->filter($collection); // queries + $queries = array_map(fn ($query) => clone $query, $queries); $filters = $this->buildFilters($queries); // permissions diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index dac4d100f..34e59e400 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1578,6 +1578,8 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, $where = []; $orders = []; + $queries = array_map(fn ($query) => clone $query, $queries); + $orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) { '$id' => '_uid', '$internalId' => '_id', @@ -1773,6 +1775,8 @@ public function count(string $collection, array $queries = [], ?int $max = null) $where = []; $limit = \is_null($max) ? '' : 'LIMIT :max'; + $queries = array_map(fn ($query) => clone $query, $queries); + $conditions = $this->getSQLConditions($queries); if(!empty($conditions)) { $where[] = $conditions; @@ -1837,6 +1841,8 @@ public function sum(string $collection, string $attribute, array $queries = [], $where = []; $limit = \is_null($max) ? '' : 'LIMIT :max'; + $queries = array_map(fn ($query) => clone $query, $queries); + foreach ($queries as $query) { $where[] = $this->getSQLCondition($query); } diff --git a/src/Database/Query.php b/src/Database/Query.php index b01534aab..6af553415 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -91,6 +91,15 @@ public function __construct(string $method, string $attribute = '', array $value $this->values = $values; } + public function __clone(): void + { + foreach ($this->values as $index => $value) { + if ($value instanceof self) { + $this->values[$index] = clone $value; + } + } + } + /** * @return string */ diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 39534c01e..f903f8af2 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -4150,6 +4150,67 @@ public function testAndNested(): void $this->assertEquals(3, $count); } + public function testNestedIDQueries(): void + { + Authorization::setRole(Role::any()->toString()); + + static::getDatabase()->createCollection('movies_nested_id', permissions: [ + Permission::create(Role::any()), + Permission::update(Role::users()) + ]); + + $this->assertEquals(true, static::getDatabase()->createAttribute('movies_nested_id', 'name', Database::VAR_STRING, 128, true)); + + static::getDatabase()->createDocument('movies_nested_id', new Document([ + '$id' => ID::custom('1'), + '$permissions' => [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + 'name' => '1', + ])); + + static::getDatabase()->createDocument('movies_nested_id', new Document([ + '$id' => ID::custom('2'), + '$permissions' => [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + 'name' => '2', + ])); + + static::getDatabase()->createDocument('movies_nested_id', new Document([ + '$id' => ID::custom('3'), + '$permissions' => [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + 'name' => '3', + ])); + + $queries = [ + Query::or([ + Query::equal('$id', ["1"]), + Query::equal('$id', ["2"]) + ]) + ]; + + $documents = static::getDatabase()->find('movies_nested_id', $queries); + $this->assertCount(2, $documents); + + // Make sure the query was not modified by reference + $this->assertEquals($queries[0]->getValues()[0]->getAttribute(), '$id'); + + $count = static::getDatabase()->count('movies_nested_id', $queries); + $this->assertEquals(2, $count); + } + /** * @depends testFind */