Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,7 @@ public function createDocuments(string $collection, array $documents): array
$batchKeys = [];
$bindValues = [];
$permissions = [];
$bindValuesPermissions = [];

foreach ($documents as $index => $document) {
$attributes = $document->getAttributes();
Expand Down Expand Up @@ -1902,6 +1903,10 @@ public function createDocuments(string $collection, array $documents): array
$permission = \str_replace('"', '', $permission);
$permission = "('{$type}', '{$permission}', :_uid_{$index} {$tenantBind})";
$permissions[] = $permission;
$bindValuesPermissions[":_uid_{$index}"] = $document->getId();
if ($this->sharedTables) {
$bindValuesPermissions[":_tenant_{$index}"] = $document->getTenant();
}
}
}
}
Expand Down Expand Up @@ -1930,11 +1935,8 @@ public function createDocuments(string $collection, array $documents): array

$stmtPermissions = $this->getPDO()->prepare($sqlPermissions);

foreach ($documents as $index => $document) {
$stmtPermissions->bindValue(":_uid_{$index}", $document->getId());
if ($this->sharedTables) {
$stmtPermissions->bindValue(":_tenant_{$index}", $document->getTenant());
}
foreach ($bindValuesPermissions as $key => $value) {
$stmtPermissions->bindValue($key, $value, $this->getPDOType($value));
}

$this->execute($stmtPermissions);
Expand Down
32 changes: 32 additions & 0 deletions tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,38 @@ public function testCreateDocuments(): void
$this->assertIsInt($document->getAttribute('bigint'));
$this->assertEquals(9223372036854775807, $document->getAttribute('bigint'));
}

// with different set of attributes
$colName = "docs_with_diff";
$database->createCollection($colName);
$database->createAttribute($colName, 'key', Database::VAR_STRING, 50, true);
$database->createAttribute($colName, 'value', Database::VAR_STRING, 50, false);
$permissions = [Permission::read(Role::any()), Permission::write(Role::any()),Permission::update(Role::any())];
$docs = [
new Document([
'$id' => 'doc1',
'key' => 'doc1',
]),
new Document([
'$id' => 'doc2',
'key' => 'doc2',
]),
new Document([
'$id' => 'doc3',
'$permissions' => $permissions,
'key' => 'doc3',
'value' => 'test'
]),
];
$this->assertEquals(3, $database->createDocuments($colName, $docs));
// we should get only one document as read permission provided to the last document only
$addedDocs = $database->find($colName);
$this->assertCount(1, $addedDocs);
$doc = $addedDocs[0];
$this->assertEquals('doc3', $doc->getId());
$this->assertNotEmpty($doc->getPermissions());
$this->assertCount(3, $doc->getPermissions());
$database->deleteCollection($colName);
}

public function testCreateDocumentsWithAutoIncrement(): void
Expand Down