Skip to content

Commit c481610

Browse files
abnegateclaude
andcommitted
(fix): convert associative arrays to Documents in relationship hooks
When inline relationship data is provided without $id (e.g., {'libraryName': 'Library 2'}), the Document constructor doesn't auto-convert it since it lacks $id/$collection. The afterDocumentCreate and afterDocumentUpdate hooks then see a plain array and throw RelationshipException for OneToOne relationships. Fix: convert non-list associative arrays to Documents before the type-checking logic, allowing auto-ID generation to work. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 84c0c74 commit c481610

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/Database/Hook/Relationships.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ public function afterDocumentCreate(Document $collection, Document $document): D
147147
$this->writeStack[] = $collection->getId();
148148

149149
try {
150+
if (\is_array($value) && ! \array_is_list($value)) {
151+
$value = new Document($value);
152+
$document->setAttribute($key, $value);
153+
}
154+
150155
if (\is_array($value)) {
151156
if (
152157
($relationType === RelationType::ManyToOne && $side === RelationSide::Parent) ||
@@ -279,6 +284,12 @@ public function afterDocumentUpdate(Document $collection, Document $old, Documen
279284
$typedRelAttr = Attribute::fromDocument($relationship);
280285
$key = $typedRelAttr->key;
281286
$value = $document->getAttribute($key);
287+
288+
if (\is_array($value) && ! \array_is_list($value)) {
289+
$value = new Document($value);
290+
$document->setAttribute($key, $value);
291+
}
292+
282293
$oldValue = $old->getAttribute($key);
283294
$rel = RelationshipVO::fromDocument($collection->getId(), $relationship);
284295
$relatedCollection = $this->db->getCollection($rel->relatedCollection);

0 commit comments

Comments
 (0)