library/Garp/Content/Api/Rest.php
protected function _combineRecords($datatype, array $records, $with) {
        $modelName = $this->_normalizeModelName($datatype);
        $rootModel = new $modelName;
        $schema = instance(new Garp_Content_Api_Rest_Schema('rest'))->getModelDetails($datatype);
        $hasOneRelations = array_filter($schema['fields'], propertyEquals('origin', 'relation'));
        $hasOneRelations = array_map(array_get('relationAlias'), $hasOneRelations);

        // Check validity of 'with'
        $unknowns = array_filter($with, callRight(not('in_array'), $hasOneRelations));
        if (count($unknowns)) {
            $err = sprintf(
                Garp_Content_Api_Rest_Schema::EXCEPTION_RELATION_NOT_FOUND, $datatype,
                current($unknowns)
            );
            throw new Garp_Content_Api_Rest_Exception($err);
        }

        $self = $this;
        return array_reduce(
            $with,
            function ($acc, $cur) use ($datatype, $schema, $self) {
                // Grab foreign key names from the relation
                $foreignKey = current(
                    array_filter(
                        $schema['fields'],
                        propertyEquals('relationAlias', $cur)
                    )
                );

                // Prepare a place for the result
                if (!isset($acc[$foreignKey['model']])) {
                    $acc[$foreignKey['model']] = array();
                }

                // Grab foreign key values
                $foreignKeyValues = array_map(array_get($foreignKey['name']), $acc[$datatype]);
                // No values to filter on? Bail.
                if (!count(array_filter($foreignKeyValues))) {
                    return $acc;
                }
                $foreignKeyValues = array_values(array_unique($foreignKeyValues));

                // Construct options object for manager
                $options = array(
                    'options'  => array('query' => array('id' => $foreignKeyValues)),
                    'datatype' => $foreignKey['model']
                );

                // Fetch with options
                $relatedRowset = current($self->_getIndex($options));
                $acc[$foreignKey['model']] = array_merge(
                    $acc[$foreignKey['model']],
                    $relatedRowset['result'][$foreignKey['model']]
                );
                return $acc;
            },
            $records
        );

    }
protected function _combineRecords($datatype, array $records, $with) {
        $modelName = $this->_normalizeModelName($datatype);
        $rootModel = new $modelName;
        $schema = instance(new Garp_Content_Api_Rest_Schema('rest'))->getModelDetails($datatype);
        $hasOneRelations = array_filter($schema['fields'], propertyEquals('origin', 'relation'));
        $hasOneRelations = array_map(array_get('relationAlias'), $hasOneRelations);

        // Check validity of 'with'
        $unknowns = array_filter($with, callRight(not('in_array'), $hasOneRelations));
        if (count($unknowns)) {
            $err = sprintf(
                Garp_Content_Api_Rest_Schema::EXCEPTION_RELATION_NOT_FOUND, $datatype,
                current($unknowns)
            );
            throw new Garp_Content_Api_Rest_Exception($err);
        }

        $self = $this;
        return array_reduce(
            $with,
            function ($acc, $cur) use ($datatype, $schema, $self) {
                // Grab foreign key names from the relation
                $foreignKey = current(
                    array_filter(
                        $schema['fields'],
                        propertyEquals('relationAlias', $cur)
                    )
                );

                // Prepare a place for the result
                if (!isset($acc[$foreignKey['model']])) {
                    $acc[$foreignKey['model']] = array();
                }

                // Grab foreign key values
                $foreignKeyValues = array_map(array_get($foreignKey['name']), $acc[$datatype]);
                // No values to filter on? Bail.
                if (!count(array_filter($foreignKeyValues))) {
                    return $acc;
                }
                $foreignKeyValues = array_values(array_unique($foreignKeyValues));

                // Construct options object for manager
                $options = array(
                    'options'  => array('query' => array('id' => $foreignKeyValues)),
                    'datatype' => $foreignKey['model']
                );

                // Fetch with options
                $relatedRowset = current($self->_getIndex($options));
                $acc[$foreignKey['model']] = array_merge(
                    $acc[$foreignKey['model']],
                    $relatedRowset['result'][$foreignKey['model']]
                );
                return $acc;
            },
            $records
        );

    }
protected function _combineRecords ( $ datatype , array $ records , $ with ) { $ modelName = $ this -> _normalizeModelName ( $ datatype ) ; $ rootModel = new $ modelName ; $ schema = instance ( new Garp_Content_Api_Rest_Schema ( 'rest' ) ) -> getModelDetails ( $ datatype ) ; $ hasOneRelations = array_filter ( $ schema [ 'fields' ] , propertyEquals ( 'origin' , 'relation' ) ) ; $ hasOneRelations = array_map ( array_get ( 'relationAlias' ) , $ hasOneRelations ) ; // Check validity of 'with' $ unknowns = array_filter ( $ with , callRight ( not ( 'in_array' ) , $ hasOneRelations ) ) ; if ( count ( $ unknowns ) ) { $ err = sprintf ( Garp_Content_Api_Rest_Schema :: EXCEPTION_RELATION_NOT_FOUND , $ datatype , current ( $ unknowns ) ) ; throw new Garp_Content_Api_Rest_Exception ( $ err ) ; } $ self = $ this ; return array_reduce ( $ with , function ( $ acc , $ cur ) use ( $ datatype , $ schema , $ self ) { // Grab foreign key names from the relation $ foreignKey = current ( array_filter ( $ schema [ 'fields' ] , propertyEquals ( 'relationAlias' , $ cur ) ) ) ; // Prepare a place for the result if ( ! isset ( $ acc [ $ foreignKey [ 'model' ] ] ) ) { $ acc [ $ foreignKey [ 'model' ] ] = array ( ) ; } // Grab foreign key values $ foreignKeyValues = array_map ( array_get ( $ foreignKey [ 'name' ] ) , $ acc [ $ datatype ] ) ; // No values to filter on? Bail. if ( ! count ( array_filter ( $ foreignKeyValues ) ) ) { return $ acc ; } $ foreignKeyValues = array_values ( array_unique ( $ foreignKeyValues ) ) ; // Construct options object for manager $ options = array ( 'options' => array ( 'query' => array ( 'id' => $ foreignKeyValues ) ) , 'datatype' => $ foreignKey [ 'model' ] ) ; // Fetch with options $ relatedRowset = current ( $ self -> _getIndex ( $ options ) ) ; $ acc [ $ foreignKey [ 'model' ] ] = array_merge ( $ acc [ $ foreignKey [ 'model' ] ] , $ relatedRowset [ 'result' ] [ $ foreignKey [ 'model' ] ] ) ; return $ acc ; } , $ records ) ; }
Get related hasOne records and combine into a single response

@param string $datatype
@param array $records
@param array $with
@return array The combined resultsets
Get related hasOne records and combine into a single response