Skip to content

Commit a340600

Browse files
committed
fixed issues with has many not having a record
1 parent 59d151a commit a340600

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/ActiveRecord.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,13 +619,15 @@ protected function &getRelation(string $name)
619619
}
620620

621621
$relation = $this->relations[$name];
622-
if (is_array($relation) === true) {
622+
if (is_array($relation) === true && isset($relation[0])) {
623623
// ActiveRecordData::BELONGS_TO etc
624624
$relation_type_or_object_name = $relation[0];
625625
$relation_class = $relation[1] ?? '';
626626
$relation_local_key = $relation[2] ?? '';
627627
$relation_array_callbacks = $relation[3] ?? [];
628628
$relation_back_reference = $relation[4] ?? '';
629+
} else {
630+
return $relation;
629631
}
630632

631633
if ($relation instanceof self || $relation_type_or_object_name instanceof self) {
@@ -643,7 +645,8 @@ protected function &getRelation(string $name)
643645
if ((!$relation instanceof self) && self::HAS_ONE === $relation_type_or_object_name) {
644646
$obj->eq($relation_local_key, $this->{$this->primaryKey})->find() && $relation_back_reference && $obj->__set($relation_back_reference, $this);
645647
} elseif (self::HAS_MANY === $relation_type_or_object_name) {
646-
$this->relations[$name] = $obj->eq($relation_local_key, $this->{$this->primaryKey})->findAll();
648+
$results = $obj->eq($relation_local_key, $this->{$this->primaryKey})->findAll();
649+
$this->relations[$name] = $results ?: [];
647650
if ($relation_back_reference) {
648651
foreach ($this->relations[$name] as $o) {
649652
$o->__set($relation_back_reference, $this);

tests/ActiveRecordPdoIntegrationTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,4 +748,33 @@ public function testBooleanParamWithArray()
748748
$sql = $record->getBuiltSql();
749749
$this->assertEquals('SELECT "user".* FROM "user" WHERE "user"."name" = :ph1 AND "user"."id" IN (TRUE,FALSE) LIMIT 1', $sql);
750750
}
751+
752+
public function testHasManyInvalidRelation()
753+
{
754+
$user = new class(new PDO('sqlite:test.db')) extends User {
755+
public array $relations = [
756+
'invalid_relation' => [] // Invalid - missing required elements
757+
];
758+
};
759+
760+
$user->name = 'demo';
761+
$user->password = md5('demo');
762+
$user->insert();
763+
764+
// Should return empty array for invalid relation instead of throwing exception
765+
$this->assertIsArray($user->invalid_relation);
766+
$this->assertEmpty($user->invalid_relation);
767+
}
768+
public function testHasManyEmptyRelation()
769+
{
770+
$user = new User(new PDO('sqlite:test.db'));
771+
$user->name = 'demo';
772+
$user->password = md5('demo');
773+
$user->insert();
774+
775+
// No contacts exist for this user
776+
$this->assertEmpty($user->contacts);
777+
$this->assertIsArray($user->contacts);
778+
$this->assertEquals(0, count($user->contacts));
779+
}
751780
}

0 commit comments

Comments
 (0)