/**
  * utility shared between getBelongsToRecord and getHasManyRecords
  * will process a related record result set and return
  * one or more individual record arrays in a larger array
  *
  * @param array $result            
  * @return array
  */
 protected function loadRelationRecords($result, \PhalconRest\API\Relation $relation)
 {
     $relatedRecords = array();
     // store all related records
     foreach ($result as $relatedRecord) {
         // reset for each run
         $relatedRecArray = array();
         // when a related record contains hasOne or a parent, merge in those fields as part of side load response
         $parent = $relation->getParent();
         if ($parent or get_class($relatedRecord) == 'Phalcon\\Mvc\\Model\\Row') {
             // process records that include joined in parent records
             foreach ($relatedRecord as $rec) {
                 // filter manyHasMany differently than other relationships
                 if ($relation->getType() == 4) {
                     // only intersted in the "end" relationship, not the intermediate
                     $intermediateModelNameSpace = $relation->getIntermediateModel();
                     if ($intermediateModelNameSpace == get_class($rec)) {
                         continue;
                     }
                 }
                 $relatedRecArray = array_merge($relatedRecArray, $this->loadAllowedColumns($rec));
             }
         } else {
             $relatedRecArray = $this->loadAllowedColumns($relatedRecord);
         }
         $relatedRecords[] = $relatedRecArray;
     }
     return $relatedRecords;
 }
예제 #2
0
 /**
  * utility shared between getBelongsToRecord and getHasManyRecords
  * will process a related record result set and return
  * one or more individual record arrays in a larger array
  *
  * @param array $result
  * @param Relation|PhalconRelation $relation
  * @return array
  */
 protected function loadRelationRecords($result, Relation $relation)
 {
     $relatedRecords = [];
     // store all related records
     foreach ($result as $relatedRecord) {
         // when a related record contains hasOne or a parent, merge in those fields as part of side load response
         $parent = $relation->getParent();
         $classType = get_class($relatedRecord);
         if ($parent and $classType == 'Phalcon\\Mvc\\Model\\Row' or $classType == 'Phalcon\\Mvc\\Model\\Row') {
             $relatedRecArray = [];
             // process records that include joined in parent records
             foreach ($relatedRecord as $rec) {
                 // filter manyHasMany differently than other relationships
                 if ($relation->getType() == PhalconRelation::HAS_MANY_THROUGH) {
                     // only interested in the "end" relationship, not the intermediate
                     $intermediateModelNameSpace = $relation->getIntermediateModel();
                     if ($intermediateModelNameSpace == get_class($rec)) {
                         continue;
                     }
                 }
                 // we don't as for parent fields here because they are already included in the complex $result
                 $newFields = $this->loadAllowedColumns($rec, false, false);
                 $relatedRecArray = array_merge($newFields, $relatedRecArray);
             }
         } else {
             // reset for each run
             $relatedRecArray = false;
             // let's inspect this for basic validity and ignore out empty records
             // think of a join on optional table
             $primaryKeyName = $relatedRecord->getPrimaryKeyName();
             if (isset($relatedRecord->{$primaryKeyName}) and $relatedRecord->{$primaryKeyName} != null) {
                 $relatedRecArray = $this->loadAllowedColumns($relatedRecord, false, false);
             }
         }
         // push if a valid record is found
         if ($relatedRecArray) {
             $relatedRecords[] = $relatedRecArray;
         }
     }
     return $relatedRecords;
 }