/** * Associate preloaded linked models to correct current models. * */ protected function _associateLinkedModels(array $cmInstances, array $lmInstances, Relation $relation) { $cmAttr = $relation->getCmAttributes(); $lmAttr = $relation->getLmAttributes(); $rName = $relation->name; foreach ($cmInstances as $cm) { foreach ($lmInstances as $lm) { if ($cm->get($cmAttr) === $lm->get($lmAttr)) { if ($relation->isToMany()) { $tmp = $cm->{$rName}; $tmp[] = $lm; $cm->{$rName} = $tmp; } else { $cm->{$rName} = $lm; break; } } } } }