For coders TYPO3 Tech Corner

[PHP] Area search in TYPO3

[PHP] Area search in TYPO3

Here is an excerpt from a possible repository. In this example the table tx_any_domain_model_any has at least the columns uid, latitude and longitude:

<?php declare(strict_types=1); namespace Vendor\Any\Domain\Repository; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Utility\GeneralUtility; /** * Class AnyRepository */ class AnyRepository { const TABLE_NAME = 'tx_any_domain_model_any'; /** * @param float $latitude * @param float $longitude * @param int $distance * @return array * @throws \Doctrine\DBAL\Exception */ public function findByRadius(float $latitude, float $longitude, int $distance): array { $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable(self::TABLE_NAME); return (array)$connection->executeQuery($this->getSqlForRadialSearch($latitude, $longitude, $distance)); } /** * @param float $latitude * @param float $longitude * @param int $distance * @return string */ protected function getSqlForRadialSearch(float $latitude, float $longitude, int $distance): string { $sql = 'SELECT uid, latitude, longitude, ( 6371 * acos( cos( radians(' . $latitude . ') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(' . $longitude . ') ) + sin( radians(' . $latitude . ') ) * sin( radians( latitude ) ) ) ) AS distance FROM ' . self::TABLE_NAME . ' HAVING distance <= ' . $distance . ' ORDER BY distance ASC;'; return $sql; } }

Back

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us