Skip to content
Merged
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
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ parameters:
count: 1
path: tests/EntityTest.php

-
message: '#^Parameter \#1 \$row of method Tests\\Support\\Models\\UserModel\:\:insert\(\) expects array\<int\|string, bool\|float\|int\|object\|string\|null\>\|object\|null, array\<string, array\|string\> given\.$#'
identifier: argument.type
count: 1
path: tests/EntityTest.php

-
message: '#^Cannot access property \$country on array\|bool\|float\|int\|object\|string\.$#'
identifier: property.nonObject
Expand Down
6 changes: 3 additions & 3 deletions src/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Relation
/**
* @var list<mixed>
*/
private array|Entity $data = [];
private array|Entity|null $data = [];

public function __construct(
public readonly RelationTypes $type,
Expand Down Expand Up @@ -61,7 +61,7 @@ public function applyConditions(): static
/**
* @param list<mixed> $data
*/
public function setData(array|Entity $data): static
public function setData(array|Entity|null $data): static
{
$this->data = $data;

Expand Down Expand Up @@ -277,7 +277,7 @@ public function filterResult(array|Entity|null $row, string $returnType): array|

public function filterResults(array|Entity $results, string $returnType): array|Entity
{
if ($this->type !== RelationTypes::belongsToMany) {
if ($results === [] || $this->type !== RelationTypes::belongsToMany) {
return $results;
}

Expand Down
26 changes: 19 additions & 7 deletions src/Traits/HasRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,14 @@ protected function relationsAfterInsert(array $eventData): array
}

foreach ($this->relations as $relationObject) {
foreach ($relationObject->getData() as $row) {
$data = $relationObject->getData();

// Skip if data is null or empty - nothing to insert
if ($data === [null] || $data === []) {
continue;
}

foreach ($data as $row) {
$row = $this->transformDataToArray($row, 'insert');
$result = $relationObject->applyWith()->model->insert(array_merge($row, [
$relationObject->foreignKey => $eventData[$this->primaryKey],
Expand Down Expand Up @@ -349,7 +356,14 @@ protected function relationsAfterUpdate(array $eventData): array
}

foreach ($this->relations as $relationObject) {
foreach ($relationObject->getData() as $row) {
$data = $relationObject->getData();

// Skip if data is null or empty - nothing to update
if ($data === [null] || $data === []) {
continue;
}

foreach ($data as $row) {
$row = $this->transformDataToArray($row, 'insert');

foreach ($eventData[$this->primaryKey] as $id) {
Expand Down Expand Up @@ -432,11 +446,9 @@ protected function getDataForRelationById(int|string $id, Relation $relation, st

$relation->applyWith()->applyRelation($id, $this->primaryKey)->applyConditions();

$results = in_array($relation->type, [RelationTypes::hasOne, RelationTypes::belongsTo], true) ?
$relation->model->first() :
$relation->model->findAll();

return $relation->filterResults($results, $this->tempReturnType);
return in_array($relation->type, [RelationTypes::hasOne, RelationTypes::belongsTo], true) ?
$relation->filterResult($relation->model->first(), $this->tempReturnType) :
$relation->filterResults($relation->model->findAll(), $this->tempReturnType);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,46 @@ public function testRelationQueriedOnlyOnce()

$this->assertSame($queryCount + 1, $this->queryCount);
}

public function testSaveNullRelationOneToOneSkipsInsert()
{
$model = model(UserModel::class);
$data = [
'username' => 'Test User',
'company_id' => '2',
'country_id' => '1',
'profile' => null,
];

$id = $model->with('profile')->insert($data);

$this->assertIsNumeric($id);

// Verify user was created but profile was not
$user = $model->with('profile')->find($id);
$this->assertInstanceOf(User::class, $user);
$this->assertSame('Test User', $user->username);
$this->assertNull($user->profile);
}

public function testSaveEmptyArrayRelationOneToManySkipsInsert()
{
$model = model(UserModel::class);
$data = [
'username' => 'Test User',
'company_id' => '2',
'country_id' => '1',
'posts' => [],
];

$id = $model->with('posts')->insert($data);

$this->assertIsNumeric($id);

// Verify user was created but no posts were created
$user = $model->with('posts')->find($id);
$this->assertInstanceOf(User::class, $user);
$this->assertSame('Test User', $user->username);
$this->assertSame([], $user->posts);
}
}