Skip to content

Commit a0b48ed

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(), Document::getTenant(), and the tenantPerDocument document paths. Skip Sequence validation for $tenant since it's a user-provided identifier, not a sequence-generated ID. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent bab244a commit a0b48ed

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/Database/Adapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ abstract class Adapter
2323

2424
protected bool $sharedTables = false;
2525

26-
protected int|string|null $tenant = null;
26+
protected string|null $tenant = null;
2727

2828
protected bool $tenantPerDocument = false;
2929

@@ -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
}
@@ -235,9 +235,9 @@ public function setTenant(int|string|null $tenant): bool
235235
*
236236
* Get tenant to use for shared tables
237237
*
238-
* @return int|string|null
238+
* @return string|null
239239
*/
240-
public function getTenant(): int|string|null
240+
public function getTenant(): string|null
241241
{
242242
return $this->tenant;
243243
}

src/Database/Database.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,9 +1250,9 @@ public function setTenant(int|string|null $tenant): static
12501250
*
12511251
* Get tenant to use if tables are shared
12521252
*
1253-
* @return int|string|null
1253+
* @return string|null
12541254
*/
1255-
public function getTenant(): int|string|null
1255+
public function getTenant(): string|null
12561256
{
12571257
return $this->adapter->getTenant();
12581258
}
@@ -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
}

src/Database/Document.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,11 @@ public function getUpdatedAt(): ?string
175175
/**
176176
* @return int|string|null
177177
*/
178-
public function getTenant(): int|string|null
178+
public function getTenant(): string|null
179179
{
180-
return $this->getAttribute('$tenant');
180+
$tenant = $this->getAttribute('$tenant');
181+
182+
return $tenant !== null ? (string) $tenant : null;
181183
}
182184

183185
/**

src/Database/Validator/Structure.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ protected function checkForInvalidAttributeValues(array $structure, array $keys)
340340

341341
switch ($type) {
342342
case Database::VAR_ID:
343+
if ($key === '$tenant') {
344+
break;
345+
}
343346
$validators[] = new Sequence($this->idAttributeType, $attribute['$id'] === '$sequence');
344347
break;
345348

0 commit comments

Comments
 (0)