Skip to content

Commit 9fabbbb

Browse files
wolf-leoliu21st
authored andcommitted
fix(relations): handle null local key values in HasMany and HasOne relations
- Added null coalescing operator to prevent undefined index errors when local key is null - Updated HasMany relation to use $pk ?? '' instead of direct array access - Updated HasOne relation to use $result->$localKey ?? '' for safe array indexing - Prevents potential PHP notices when foreign key values are null in database records
1 parent 4e719e3 commit 9fabbbb

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/model/relation/HasMany.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
9898
// 关联数据封装
9999
foreach ($resultSet as $result) {
100100
$pk = $result->$localKey;
101-
if (!isset($data[$pk])) {
102-
$data[$pk] = [];
101+
if (!isset($data[$pk ?? ''])) {
102+
$data[$pk ?? ''] = [];
103103
}
104104

105-
$result->setRelation($relation, $this->resultSetBuild($data[$pk]));
105+
$result->setRelation($relation, $this->resultSetBuild($data[$pk ?? '']));
106106
}
107107
}
108108
}

src/model/relation/HasOne.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ protected function eagerlySet(array &$resultSet, string $relation, array $subRel
240240
// 关联数据封装
241241
foreach ($resultSet as $result) {
242242
// 关联模型
243-
if (!isset($data[$result->$localKey])) {
243+
if (!isset($data[$result->$localKey ?? ''])) {
244244
$relationModel = $defaultModel;
245245
} else {
246-
$relationModel = $data[$result->$localKey];
246+
$relationModel = $data[$result->$localKey ?? ''];
247247
}
248248
// 设置关联属性
249249
if (!empty($this->bindAttr) && $relationModel) {

0 commit comments

Comments
 (0)