Skip to content

Commit

Permalink
check if toArray method exists
Browse files Browse the repository at this point in the history
  • Loading branch information
jturbide committed Oct 25, 2023
1 parent cb84393 commit 1b1c139
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Mvc/Model/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,13 +873,13 @@ public function relatedToArray(?array $relationFields = null): array
$relatedColumns = $relationFields[$attributeField] ?? null;

// Run toArray on related records
if ($related instanceof ModelInterface) {
if ($related instanceof ModelInterface && method_exists($related, 'toArray')) {
$ret[$attributeField] = $related->toArray($relatedColumns);
}
elseif (is_iterable($related)) {
$ret[$attributeField] = [];
foreach ($related as $entity) {
if ($entity instanceof ModelInterface) {
if ($entity instanceof ModelInterface && method_exists($entity, 'toArray')) {
$ret[$attributeField][] = $entity->toArray($relatedColumns);
}
elseif (is_array($entity)) {
Expand Down

0 comments on commit 1b1c139

Please sign in to comment.