Merge pull request #12 from utopia-php/feat-multi-update

Add multi param to update to allow single or multiple updates
This commit is contained in:
Jake Barnby authored and GitHub committed 2023-03-22 23:44:29 +13:00
commit b6dfb31b93
3 files changed
+50 -17

No files matched your search

-15
View File
@@ -1,15 +0,0 @@
<?php
namespace Utopia\Mongo;
abstract class Adapter
{
abstract public function isConnected(): bool;
abstract public function connect(
string $host,
int $port = null,
float $timeout = null
): bool;
abstract public function send(mixed $data): int|bool;
abstract public function recv(int $timeout = null): string|bool;
}
+3 -2
View File
@@ -471,11 +471,12 @@ class Client
* @param array $where
* @param array $updates
* @param array $options
* @param bool $multi
*
* @return Client
* @throws Exception
*/
public function update(string $collection, array $where = [], array $updates = [], array $options = []): self
public function update(string $collection, array $where = [], array $updates = [], array $options = [], bool $multi = false): self
{
$cleanUpdates = [];
@@ -493,7 +494,7 @@ class Client
[
'q' => $this->toObject($where),
'u' => $this->toObject($cleanUpdates),
'multi' => false,
'multi' => $multi,
'upsert' => false
]
]
+47
View File
@@ -108,6 +108,53 @@ class MongoTest extends TestCase
self::assertIsObject($doc->date_object); // Todo: This is not working can't retrieve the object back
}
public function testUpdateDocument(): void
{
$this->getDatabase()->insert(
'movies',
[
'name' => 'Armageddon',
'country' => 'AUS',
'language' => 'English'
]
);
$this->getDatabase()->update(
'movies',
['name' => 'Armageddon'],
['$set' => ['name' => 'Armageddon 2']]
);
$doc = $this->getDatabase()->find(
'movies',
['name' => 'Armageddon 2']
)->cursor->firstBatch ?? [];
self::assertCount(1, $doc);
}
public function testUpdateMultipleDocuments(): void
{
$this->getDatabase()->update(
'movies',
['name' => 'Armageddon 2'],
['$set' => ['name' => 'Armageddon']]
);
$this->getDatabase()->update(
'movies',
['name' => 'Armageddon'],
['$rename' => ['name' => 'title']],
multi: true
);
$docs = $this->getDatabase()->find(
'movies',
['title' => 'Armageddon']
)->cursor->firstBatch ?? [];
self::assertCount(2, $docs);
}
public function testDriverException()
{
self::expectException('MongoDB\Driver\Exception\InvalidArgumentException');