Skip to content

Commit ac7da4f

Browse files
loks0nclaude
andcommitted
fix: normalize tenant to string to match database round-trip type
setTenant() accepts int|string|null but PDO returns tenant as a string after storage. The strict !== comparisons in getCollection() then fail because (int)1 !== "1", causing "Collection not found" errors. The Sequence validator also rejects integer tenant values since $tenant has type VAR_ID which requires strings. Cast tenant to string in Adapter::setTenant() and in the tenantPerDocument document paths where tenant is set directly on the document. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent bab244a commit ac7da4f

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/Database/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public function getSharedTables(): bool
225225
*/
226226
public function setTenant(int|string|null $tenant): bool
227227
{
228-
$this->tenant = $tenant;
228+
$this->tenant = $tenant !== null ? (string) $tenant : null;
229229

230230
return true;
231231
}

src/Database/Database.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5503,12 +5503,16 @@ public function createDocument(string $collection, Document $document): Document
55035503

55045504
if ($this->adapter->getSharedTables()) {
55055505
if ($this->adapter->getTenantPerDocument()) {
5506+
$tenant = $document->getTenant();
55065507
if (
55075508
$collection->getId() !== static::METADATA
5508-
&& $document->getTenant() === null
5509+
&& $tenant === null
55095510
) {
55105511
throw new DatabaseException('Missing tenant. Tenant must be set when tenant per document is enabled.');
55115512
}
5513+
if ($tenant !== null) {
5514+
$document->setAttribute('$tenant', (string) $tenant);
5515+
}
55125516
} else {
55135517
$document->setAttribute('$tenant', $this->adapter->getTenant());
55145518
}
@@ -5621,9 +5625,11 @@ public function createDocuments(
56215625

56225626
if ($this->adapter->getSharedTables()) {
56235627
if ($this->adapter->getTenantPerDocument()) {
5624-
if ($document->getTenant() === null) {
5628+
$tenant = $document->getTenant();
5629+
if ($tenant === null) {
56255630
throw new DatabaseException('Missing tenant. Tenant must be set when tenant per document is enabled.');
56265631
}
5632+
$document->setAttribute('$tenant', (string) $tenant);
56275633
} else {
56285634
$document->setAttribute('$tenant', $this->adapter->getTenant());
56295635
}
@@ -7198,9 +7204,11 @@ public function upsertDocumentsWithIncrease(
71987204

71997205
if ($this->adapter->getSharedTables()) {
72007206
if ($this->adapter->getTenantPerDocument()) {
7201-
if ($document->getTenant() === null) {
7207+
$tenant = $document->getTenant();
7208+
if ($tenant === null) {
72027209
throw new DatabaseException('Missing tenant. Tenant must be set when tenant per document is enabled.');
72037210
}
7211+
$document->setAttribute('$tenant', (string) $tenant);
72047212
if (!$old->isEmpty() && $old->getTenant() !== $document->getTenant()) {
72057213
throw new DatabaseException('Tenant cannot be changed.');
72067214
}

0 commit comments

Comments
 (0)