protected function loadResourcesFromQueryBuilder($resourceQueryBuilder, $collection) { $resources = $resourceQueryBuilder->get(); $returnedResources = []; if ($resources) { foreach ($resources as $resource) { $resource = json_decode(json_encode($resource), true); $returnedResource = new ForestResource($collection, $this->formatResource($resource, $collection)); $resourceId = $returnedResource->getId(); $relationships = $collection->getRelationships(); if (count($relationships)) { foreach ($relationships as $tableReference => $field) { $relatedCollection = $this->findCollection($field->getPivot()->getIntermediaryTableName()); $relationship = new ForestRelationship(); $relationship->setType($relatedCollection->getName()); $relationship->setEntityClassName($relatedCollection->getEntityClassName()); $relationship->setFieldName($field->getField()); $relationship->setIdentifier($relatedCollection->getIdentifier()); if ($field->isTypeToOne()) { $relationship->setId($resource[$field->getPivot()->getSourceIdentifier()]); } $returnedResource->addRelationship($relationship); } foreach ($returnedResource->getRelationships() as $relationship) { if ($relationship->getId()) { $relatedCollection = $this->findCollection($relationship->getType()); list($resourceToInclude, $resultSet) = $this->loadResource($relationship->getId(), $relatedCollection); if ($resourceToInclude) { $resourceToInclude->setType($relationship->getType()); $returnedResource->includeResource($resourceToInclude); } } } } $returnedResources[] = $returnedResource; } } return $returnedResources; }
/** * @param $resourceQueryBuilder * @param $collection * @return Resource[] * @throws CollectionNotFoundException */ protected function loadResourcesFromQueryBuilder($resourceQueryBuilder, $collection) { $resources = $resourceQueryBuilder->getQuery()->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); $returnedResources = array(); if ($resources) { foreach ($resources as $resource) { $returnedResource = new ForestResource($collection, $this->formatResource($resource, $collection)); $resourceId = $returnedResource->getId(); $associatedRepository = $this->getEntityManager()->getRepository($collection->getEntityClassName()); $relationships = $collection->getRelationships(); if (count($relationships)) { foreach ($relationships as $tableReference => $field) { $relatedCollection = $this->findCollection($tableReference); $relationship = new ForestRelationship(); $relationship->setType($relatedCollection->getName()); $relationship->setEntityClassName($relatedCollection->getEntityClassName()); $relationship->setFieldName($field->getField()); $relationship->setIdentifier($relatedCollection->getIdentifier()); if ($field->isTypeToOne()) { $relationship->setId($resource[$field->getForeignKey()]); } $returnedResource->addRelationship($relationship); } foreach ($returnedResource->getRelationships() as $relationship) { if ($relationship->getId()) { $relatedCollection = $this->findCollection($relationship->getType()); $relatedRepository = $this->getEntityManager()->getRepository($relationship->getEntityClassName()); list($resourceToInclude, $rs) = $this->loadResource($relationship->getId(), $relatedCollection, $relatedRepository); if ($resourceToInclude) { $resourceToInclude->setType($relationship->getType()); $returnedResource->includeResource($resourceToInclude); } } } } $returnedResources[] = $returnedResource; } } return $returnedResources; }