Skip to content

Commit a07ec0e

Browse files
abnegateclaude
andcommitted
(fix): restore Document tenant int cast, validate relationship $id, fix PHPStan types
- Restore numeric-to-int cast in Document::getTenant() that was removed in 89e7afc. MongoDB is type-strict: stored int 999 won't match query string "999". The cast ensures tenant values stay as ints after the ColumnType::Id string cast in castingAfter. - Add validation for null $id on relationship Document values during update, throwing RelationshipException instead of auto-creating. - Fix PHPStan annotations: use inline @phpstan-ignore for Document constructor calls, update MongoTenantFilter closure return type. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ed0b48a commit a07ec0e

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/Database/Document.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,13 @@ public function getUpdatedAt(): ?string
241241
*/
242242
public function getTenant(): int|string|null
243243
{
244-
return $this->getAttribute('$tenant');
244+
$tenant = $this->getAttribute('$tenant');
245+
246+
if (\is_numeric($tenant)) {
247+
return (int) $tenant;
248+
}
249+
250+
return $tenant;
245251
}
246252

247253
/**

src/Database/Hook/MongoTenantFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MongoTenantFilter implements Read
1717
/**
1818
* @param int|string|null $tenant The current tenant ID
1919
* @param bool $sharedTables Whether shared tables mode is enabled
20-
* @param Closure(string, array<int|string>=): (int|string|null|array<string, array<int|string>>) $getTenantFilters Closure that returns tenant filter values for a collection
20+
* @param Closure(string, array<int|string>=): (int|string|null|array<string, array<int|string|null>>) $getTenantFilters Closure that returns tenant filter values for a collection
2121
*/
2222
public function __construct(
2323
private int|string|null $tenant,

src/Database/Hook/Relationships.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ public function afterDocumentCreate(Document $collection, Document $document): D
148148

149149
try {
150150
if (\is_array($value) && ! \array_is_list($value)) {
151-
/** @var array<string, mixed> $value */
152-
$value = new Document($value);
151+
$value = new Document($value); // @phpstan-ignore argument.type
153152
$document->setAttribute($key, $value);
154153
}
155154

@@ -287,8 +286,7 @@ public function afterDocumentUpdate(Document $collection, Document $old, Documen
287286
$value = $document->getAttribute($key);
288287

289288
if (\is_array($value) && ! \array_is_list($value)) {
290-
/** @var array<string, mixed> $value */
291-
$value = new Document($value);
289+
$value = new Document($value); // @phpstan-ignore argument.type
292290
$document->setAttribute($key, $value);
293291
}
294292

@@ -536,6 +534,10 @@ public function afterDocumentUpdate(Document $collection, Document $old, Documen
536534
}
537535
$this->db->purgeCachedDocument($relatedCollection->getId(), $value);
538536
} elseif ($value instanceof Document) {
537+
if (empty($value->getId())) {
538+
throw new RelationshipException('Invalid relationship value. Document must have a valid $id.');
539+
}
540+
539541
$related = $this->db->skipRelationships(
540542
fn () => $this->db->getDocument($relatedCollection->getId(), $value->getId(), [Query::select(['$id'])])
541543
);

0 commit comments

Comments
 (0)