private function hasMany()
    {
        // Return if there are no relationships of that type
        if (!isset($this->relationships['hasMany']) || empty($this->relationships['hasMany'])) {
            return $this;
        }

        // Get current records IDs
        $ids = array_column($this->records, 'id');

        // Return if no records where found
        if (empty($ids)) {
            return $this;
        }

        // Make a associative array from current records
        $records = array_combine($ids, $this->records);

        // Iterate relationships
        foreach ($this->relationships['hasMany'] as $join) {

            // Get relationship results trought the model
            if (isset($join['model'])) {
                if (isset($this->models) && isset($this->models[$join['model']])) {
                    $instance = $this->models[$join['model']];
                } else {
                    $instance = new $join['model'];
                }
                $childs = $instance->whereIn($join['foreignKey'], $ids)->limit($join['limit'])->orderBy($join['orderBy'])->get();
            }

            // Or get relationship results by database
            if (!isset($join['model'])) {
                $join['orderBy'] = isset($join['orderBy'])? $join['orderBy'] : 'id';
                $childs = $this->db()->table($join['table'])->select(array_merge([$join['foreignKey']], $join['columns']))->whereIn($join['foreignKey'], $ids)->limit($join['limit'])->orderBy($join['orderBy'])->get();
            }

            // Group them by foreign key
            if ($childs) {
                foreach ($childs as $child) {
                    $key = $join['foreignKey'];
                    $id = $child[$key];

                    // If the ID was not required as a column to show, leave it out
                    if (!in_array($key, $join['columns'])) {
                        unset($child[$key]);
                    }

                    // Save the result
                    $join['results'][$id][] = $child;
                }
            }

            // Add relationship results to the main collection
            foreach ($records as $key => $value) {
                $id = $value[$join['localKey']];
                $records[$id][$join['alias']] = (isset($join['results'][$id]))? $join['results'][$value[$join['localKey']]] : [];
            }
        }

        // Save the records with the resolved relationships
        $this->records = array_values($records);

        // Return model instance
        return $this;
    }
private function hasMany()
    {
        // Return if there are no relationships of that type
        if (!isset($this->relationships['hasMany']) || empty($this->relationships['hasMany'])) {
            return $this;
        }

        // Get current records IDs
        $ids = array_column($this->records, 'id');

        // Return if no records where found
        if (empty($ids)) {
            return $this;
        }

        // Make a associative array from current records
        $records = array_combine($ids, $this->records);

        // Iterate relationships
        foreach ($this->relationships['hasMany'] as $join) {

            // Get relationship results trought the model
            if (isset($join['model'])) {
                if (isset($this->models) && isset($this->models[$join['model']])) {
                    $instance = $this->models[$join['model']];
                } else {
                    $instance = new $join['model'];
                }
                $childs = $instance->whereIn($join['foreignKey'], $ids)->limit($join['limit'])->orderBy($join['orderBy'])->get();
            }

            // Or get relationship results by database
            if (!isset($join['model'])) {
                $join['orderBy'] = isset($join['orderBy'])? $join['orderBy'] : 'id';
                $childs = $this->db()->table($join['table'])->select(array_merge([$join['foreignKey']], $join['columns']))->whereIn($join['foreignKey'], $ids)->limit($join['limit'])->orderBy($join['orderBy'])->get();
            }

            // Group them by foreign key
            if ($childs) {
                foreach ($childs as $child) {
                    $key = $join['foreignKey'];
                    $id = $child[$key];

                    // If the ID was not required as a column to show, leave it out
                    if (!in_array($key, $join['columns'])) {
                        unset($child[$key]);
                    }

                    // Save the result
                    $join['results'][$id][] = $child;
                }
            }

            // Add relationship results to the main collection
            foreach ($records as $key => $value) {
                $id = $value[$join['localKey']];
                $records[$id][$join['alias']] = (isset($join['results'][$id]))? $join['results'][$value[$join['localKey']]] : [];
            }
        }

        // Save the records with the resolved relationships
        $this->records = array_values($records);

        // Return model instance
        return $this;
    }
private function hasMany ( ) { // Return if there are no relationships of that type if ( ! isset ( $ this -> relationships [ 'hasMany' ] ) || empty ( $ this -> relationships [ 'hasMany' ] ) ) { return $ this ; } // Get current records IDs $ ids = array_column ( $ this -> records , 'id' ) ; // Return if no records where found if ( empty ( $ ids ) ) { return $ this ; } // Make a associative array from current records $ records = array_combine ( $ ids , $ this -> records ) ; // Iterate relationships foreach ( $ this -> relationships [ 'hasMany' ] as $ join ) { // Get relationship results trought the model if ( isset ( $ join [ 'model' ] ) ) { if ( isset ( $ this -> models ) && isset ( $ this -> models [ $ join [ 'model' ] ] ) ) { $ instance = $ this -> models [ $ join [ 'model' ] ] ; } else { $ instance = new $ join [ 'model' ] ; } $ childs = $ instance -> whereIn ( $ join [ 'foreignKey' ] , $ ids ) -> limit ( $ join [ 'limit' ] ) -> orderBy ( $ join [ 'orderBy' ] ) -> get ( ) ; } // Or get relationship results by database if ( ! isset ( $ join [ 'model' ] ) ) { $ join [ 'orderBy' ] = isset ( $ join [ 'orderBy' ] ) ? $ join [ 'orderBy' ] : 'id' ; $ childs = $ this -> db ( ) -> table ( $ join [ 'table' ] ) -> select ( array_merge ( [ $ join [ 'foreignKey' ] ] , $ join [ 'columns' ] ) ) -> whereIn ( $ join [ 'foreignKey' ] , $ ids ) -> limit ( $ join [ 'limit' ] ) -> orderBy ( $ join [ 'orderBy' ] ) -> get ( ) ; } // Group them by foreign key if ( $ childs ) { foreach ( $ childs as $ child ) { $ key = $ join [ 'foreignKey' ] ; $ id = $ child [ $ key ] ; // If the ID was not required as a column to show, leave it out if ( ! in_array ( $ key , $ join [ 'columns' ] ) ) { unset ( $ child [ $ key ] ) ; } // Save the result $ join [ 'results' ] [ $ id ] [ ] = $ child ; } } // Add relationship results to the main collection foreach ( $ records as $ key => $ value ) { $ id = $ value [ $ join [ 'localKey' ] ] ; $ records [ $ id ] [ $ join [ 'alias' ] ] = ( isset ( $ join [ 'results' ] [ $ id ] ) ) ? $ join [ 'results' ] [ $ value [ $ join [ 'localKey' ] ] ] : [ ] ; } } // Save the records with the resolved relationships $ this -> records = array_values ( $ records ) ; // Return model instance return $ this ; }
Resolves one to many relationships

@return object Model instance
Resolves one to many relationships