/** * Finds countries by territory * * @param \SJBR\StaticInfoTables\Domain\Model\Territory $territory * * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array */ public function findByTerritory(\SJBR\StaticInfoTables\Domain\Model\Territory $territory) { $unCodeNumbers = array($territory->getUnCodeNumber()); // Get UN code numbers of subterritories (recursively) $subterritories = $this->territoryRepository->findWithinTerritory($territory); foreach ($subterritories as $subterritory) { $unCodeNumbers[] = $subterritory->getUnCodeNumber(); } $query = $this->createQuery(); $query->matching($query->in('parentTerritoryUnCodeNumber', $unCodeNumbers)); return $query->execute(); }
/** * Finds all territories within a territory recursively * * @param \SJBR\StaticInfoTables\Domain\Model\Territory $territory * @param array $unCodeNumbers array of UN territory code numbers used for recursive retrieval of sub-territories * * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array */ public function findWithinTerritory(\SJBR\StaticInfoTables\Domain\Model\Territory $territory, &$unCodeNumbers = array()) { if (empty($unCodeNumbers)) { $unCodeNumbers = array($territory->getUnCodeNumber()); } $initialCount = count($unCodeNumbers); $query = $this->createQuery(); $query->matching($query->in('parentTerritoryUnCodeNumber', $unCodeNumbers)); $territories = $query->execute(); // Get UN code numbers of new subterritories foreach ($territories as $subterritory) { $unCodeNumbers[] = $subterritory->getUnCodeNumber(); } $unCodeNumbers = array_unique($unCodeNumbers); // Call recursively until no additional subterritories are found if (count($unCodeNumbers) > $initialCount) { $territories = $this->findWithinTerritory($territory, $unCodeNumbers); } return $territories; }