Skip to content

Commit

Permalink
Added code to make loading data for hasOne & belongsTo relationships …
Browse files Browse the repository at this point in the history
…to be set to an empty array when there's no matching relational data.
  • Loading branch information
rotimi committed Nov 27, 2024
1 parent eb194de commit 1061e55
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/LeanOrm/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,11 @@ protected function loadHasOne(
// record per parent record since this is a hasOne
// relationship. That's why we are doing
// $matching_related_rows[0]
$parent_data[$p_rec_key]->setRelatedData($rel_name, $matching_related_rows[0]);
$parent_data[$p_rec_key]->setRelatedData(
$rel_name,
(\count($matching_related_rows) > 0)
? $matching_related_rows[0] : []
);

} else {

Expand All @@ -912,7 +916,9 @@ protected function loadHasOne(
// $matching_related_rows[0]

//the current row must be an array
$parent_data[$p_rec_key][$rel_name] = $matching_related_rows[0];
$parent_data[$p_rec_key][$rel_name] =
(\count($matching_related_rows) > 0)
? $matching_related_rows[0] : [];
}
} // foreach( $parent_data as $p_rec_key => $parent_record )

Expand All @@ -928,7 +934,10 @@ protected function loadHasOne(
);

//stitch the related data to the parent record
$parent_data->setRelatedData($rel_name, array_shift($related_data));
$parent_data->setRelatedData(
$rel_name,
(\count($related_data) > 0) ? \array_shift($related_data) : []
);
} // else if ($parent_data instanceof \GDAO\Model\RecordInterface)
} // if( array_key_exists($rel_name, $this->relations) )
}
Expand Down

0 comments on commit 1061e55

Please sign in to comment.