FOF30\Model\DataModel::eagerLoad PHP Method

eagerLoad() public method

Eager loads the provided relations and assigns their data to a data collection
public eagerLoad ( Collection &$dataCollection, array $relations = null )
$dataCollection FOF30\Model\DataModel\Collection The data collection on which the eager loaded relations will be applied
$relations array The relations to eager load. Leave empty to use the already defined relations
    public function eagerLoad(DataCollection &$dataCollection, array $relations = null)
    {
        if (empty($relations)) {
            $relations = $this->eagerRelations;
        }
        // Apply eager loaded relations
        if ($dataCollection->count() && !empty($relations)) {
            $relationManager = $this->getRelations();
            foreach ($relations as $relation => $callback) {
                // Did they give us a relation name without a callback?
                if (!is_callable($callback) && is_string($callback) && !empty($callback)) {
                    $relation = $callback;
                    $callback = null;
                }
                $relationData = $relationManager->getData($relation, $callback, $dataCollection);
                $foreignKeyMap = $relationManager->getForeignKeyMap($relation);
                /** @var DataModel $item */
                foreach ($dataCollection as $item) {
                    $item->getRelations()->setDataFromCollection($relation, $relationData, $foreignKeyMap);
                }
            }
        }
        return $this;
    }