src/PicORM/Model.php
private function _getRelation(array $configRelation, $callArgs)
    {
        $where      = $order = array();
        $limitStart = $limitEnd = null;

        // extract find criteria from args
        if (isset($callArgs[0]) && is_array($callArgs[0])) {
            $where = $callArgs[0];
        }

        // extract order from args
        if (isset($callArgs[1]) && is_array($callArgs[1])) {
            $order = $callArgs[1];
        }

        // extract limit from args
        if (isset($callArgs[2]) && is_numeric($callArgs[2])) {
            $limitStart = $callArgs[2];
        }
        if (isset($callArgs[3]) && is_numeric($callArgs[3])) {
            $limitEnd = $callArgs[3];
        }

        $relationValue = null;
        switch ($configRelation['typeRelation']) {
            case self::ONE_TO_ONE:
                $classRelation = $configRelation['classRelation'];
                if (is_subclass_of($classRelation, 'PicORM\Model')) {
                    // add model relation relation field to where
                    $where = array_merge(
                        $where,
                        array($configRelation['targetField'] => $this->{$configRelation['sourceField']})
                    );

                    // grab the related model
                    $relationValue = $classRelation::findOne($where, $order);
                } else {
                    throw new Exception(sprintf("%s must be a subclass of PicORM\Model", $classRelation));
                }
                break;
            case self::ONE_TO_MANY:
                $classRelation = $configRelation['classRelation'];
                if (is_subclass_of($classRelation, 'PicORM\Model')) {
                    // add model relation relation field to where
                    $where = array_merge(
                        $where,
                        array($configRelation['targetField'] => $this->{$configRelation['sourceField']})
                    );

                    // grab the related models
                    $relationValue = $classRelation::find($where, $order, $limitStart, $limitEnd);
                } else {
                    throw new Exception(sprintf("%s must be a subclass of PicORM\Model", $classRelation));
                }
                break;
            case self::MANY_TO_MANY:
                $classRelation = $configRelation['classRelation'];
                if (is_subclass_of($classRelation, 'PicORM\Model')) {
                    // create the select query for related models using relation table
                    $selectRelations = new InternalQueryHelper();
                    $selectRelations
                        ->select("t.*")
                        ->from($classRelation::formatTableNameMySQL(), 't')
                        ->innerJoin(
                        $configRelation['relationTable'],
                        $configRelation['relationTable'] . "." . $configRelation['targetField'] . " = t." . $configRelation['targetField']
                        );
                    if($order !== null) {
                        foreach ($order as $orderField => $orderVal) {
                            $selectRelations->orderBy($orderField, $orderVal);
                        }
                    }
                    $selectRelations -> limit($limitStart, $limitEnd);

                    $where = $selectRelations->prefixWhereWithTable($where,'t');
                    $selectRelations->buildWhereFromArray($where);
                    // check one to one relation with auto get fields
                    // and append needed fields to select
                    $nbRelation = 0;
                    foreach ($classRelation::$_relations as $uneRelation) {
                        if ($uneRelation['typeRelation'] == self::ONE_TO_ONE
                            && count($uneRelation['autoGetFields']) > 0
                        ) {
                            // add auto get fields to select
                            foreach ($uneRelation['autoGetFields'] as &$oneField) {
                                $oneField = 'rel' . $nbRelation . "." . $oneField;
                            }

                            // add fields to select
                            $selectRelations->select($uneRelation['autoGetFields']);

                            // add query join corresponding to the relation
                            $relationFieldName = 'rel' . $nbRelation . '.`' . $uneRelation['targetField'];
                            $relationField     = 't' . '.`' . $uneRelation['sourceField'] . '`';
                            $selectRelations
                                ->leftJoin(
                                $uneRelation['classRelation']::formatTableNameMySQL() . ' rel' . $nbRelation,
                                $relationFieldName . '` = ' . $relationField
                                );

                            // increment relation count used in prefix
                            $nbRelation++;
                        }
                    }

                    $relatedFieldName = "`" . $configRelation['relationTable'] . "`." . $configRelation['sourceField'];
                    $conditionValue   = $this->{$configRelation['sourceField']};
                    $selectRelations->buildWhereFromArray(array($relatedFieldName => $conditionValue));

                    // create collection with this model datasource instance
                    // hydrating $classRelation model and using $selectRelation query helper
                    $relationValue = new Collection(static::getDataSource(), $selectRelations, $classRelation);
                } else {
                    throw new Exception(sprintf("%s must be a subclass of PicORM\Model", $classRelation));
                }
                break;
        }

        return $relationValue;
    }
private function _getRelation(array $configRelation, $callArgs)
    {
        $where      = $order = array();
        $limitStart = $limitEnd = null;

        // extract find criteria from args
        if (isset($callArgs[0]) && is_array($callArgs[0])) {
            $where = $callArgs[0];
        }

        // extract order from args
        if (isset($callArgs[1]) && is_array($callArgs[1])) {
            $order = $callArgs[1];
        }

        // extract limit from args
        if (isset($callArgs[2]) && is_numeric($callArgs[2])) {
            $limitStart = $callArgs[2];
        }
        if (isset($callArgs[3]) && is_numeric($callArgs[3])) {
            $limitEnd = $callArgs[3];
        }

        $relationValue = null;
        switch ($configRelation['typeRelation']) {
            case self::ONE_TO_ONE:
                $classRelation = $configRelation['classRelation'];
                if (is_subclass_of($classRelation, 'PicORM\Model')) {
                    // add model relation relation field to where
                    $where = array_merge(
                        $where,
                        array($configRelation['targetField'] => $this->{$configRelation['sourceField']})
                    );

                    // grab the related model
                    $relationValue = $classRelation::findOne($where, $order);
                } else {
                    throw new Exception(sprintf("%s must be a subclass of PicORM\Model", $classRelation));
                }
                break;
            case self::ONE_TO_MANY:
                $classRelation = $configRelation['classRelation'];
                if (is_subclass_of($classRelation, 'PicORM\Model')) {
                    // add model relation relation field to where
                    $where = array_merge(
                        $where,
                        array($configRelation['targetField'] => $this->{$configRelation['sourceField']})
                    );

                    // grab the related models
                    $relationValue = $classRelation::find($where, $order, $limitStart, $limitEnd);
                } else {
                    throw new Exception(sprintf("%s must be a subclass of PicORM\Model", $classRelation));
                }
                break;
            case self::MANY_TO_MANY:
                $classRelation = $configRelation['classRelation'];
                if (is_subclass_of($classRelation, 'PicORM\Model')) {
                    // create the select query for related models using relation table
                    $selectRelations = new InternalQueryHelper();
                    $selectRelations
                        ->select("t.*")
                        ->from($classRelation::formatTableNameMySQL(), 't')
                        ->innerJoin(
                        $configRelation['relationTable'],
                        $configRelation['relationTable'] . "." . $configRelation['targetField'] . " = t." . $configRelation['targetField']
                        );
                    if($order !== null) {
                        foreach ($order as $orderField => $orderVal) {
                            $selectRelations->orderBy($orderField, $orderVal);
                        }
                    }
                    $selectRelations -> limit($limitStart, $limitEnd);

                    $where = $selectRelations->prefixWhereWithTable($where,'t');
                    $selectRelations->buildWhereFromArray($where);
                    // check one to one relation with auto get fields
                    // and append needed fields to select
                    $nbRelation = 0;
                    foreach ($classRelation::$_relations as $uneRelation) {
                        if ($uneRelation['typeRelation'] == self::ONE_TO_ONE
                            && count($uneRelation['autoGetFields']) > 0
                        ) {
                            // add auto get fields to select
                            foreach ($uneRelation['autoGetFields'] as &$oneField) {
                                $oneField = 'rel' . $nbRelation . "." . $oneField;
                            }

                            // add fields to select
                            $selectRelations->select($uneRelation['autoGetFields']);

                            // add query join corresponding to the relation
                            $relationFieldName = 'rel' . $nbRelation . '.`' . $uneRelation['targetField'];
                            $relationField     = 't' . '.`' . $uneRelation['sourceField'] . '`';
                            $selectRelations
                                ->leftJoin(
                                $uneRelation['classRelation']::formatTableNameMySQL() . ' rel' . $nbRelation,
                                $relationFieldName . '` = ' . $relationField
                                );

                            // increment relation count used in prefix
                            $nbRelation++;
                        }
                    }

                    $relatedFieldName = "`" . $configRelation['relationTable'] . "`." . $configRelation['sourceField'];
                    $conditionValue   = $this->{$configRelation['sourceField']};
                    $selectRelations->buildWhereFromArray(array($relatedFieldName => $conditionValue));

                    // create collection with this model datasource instance
                    // hydrating $classRelation model and using $selectRelation query helper
                    $relationValue = new Collection(static::getDataSource(), $selectRelations, $classRelation);
                } else {
                    throw new Exception(sprintf("%s must be a subclass of PicORM\Model", $classRelation));
                }
                break;
        }

        return $relationValue;
    }
Get a relation value using magic getter

@param array $configRelation - Relation configuration
@param array $callArgs       - Models instance

@throw Exception

@return null