Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,12 @@ private function handleStandardHasOneRelation(HasOne $hasOne, object|array $rela
}

// TODO: we might need to bake this into the naming strategy class
$foreignKeyColumn = Intl\singularize_last_word($ownerModel->getTableName()) . '_' . $ownerModel->getPrimaryKey();
$foreignKeyProperty = Intl\singularize_last_word($ownerModel->getTableName());
$foreignKeyColumn = $foreignKeyProperty . '_' . $ownerModel->getPrimaryKey();

$preparedData = is_array($relation)
? [...$relation, ...[$foreignKeyColumn => $parentId->value]]
: [...$this->convertObjectToArray($relation), ...[$foreignKeyColumn => $parentId->value]];
: [...$this->convertObjectToArray($relation, [$foreignKeyProperty]), ...[$foreignKeyColumn => $parentId->value]];

$relatedModelQuery = new InsertQueryBuilder(
model: $hasOne->property->getType()->asClass(),
Expand Down
20 changes: 20 additions & 0 deletions tests/Integration/Database/Builder/IsDatabaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ public function test_all_with_relations(): void
$this->assertEquals(1, $book->author->id->value);
}

public function test_create_with_hasone_relation(): void
{
$this->database->migrate(
CreateMigrationsTable::class,
CreatePublishersTable::class,
CreateAuthorTable::class,
CreateBookTable::class,
CreateIsbnTable::class,
);

$book = Book::create(
title: 'Book Title',
isbn: Isbn::new(value: '123-456-789'),
);

$book = Book::findById($book->id)->load('isbn');

$this->assertSame('123-456-789', $book->isbn->value);
}

public function test_missing_relation_exception(): void
{
$this->database->migrate(
Expand Down
Loading