如何用 TYPO3 8.7 做一个复杂的 Doctrine select?

How to do a complex Doctrine select with TYPO3 8.7?

我尝试使用 Doctrine 将遗留 MySQL 查询迁移到 TYPO3 8.7。

遗留 SQL 声明:

SELECT
tt_address.*,
( 6371.41 * acos( cos( radians('.$lat.') ) * cos( radians(tt_address.tx_geosearch_lat ) ) * cos( radians(tt_address.tx_geosearch_lng ) - radians('.$lng.') ) + sin( radians('.$lat.') ) * sin( radians(tt_address.tx_geosearch_lat ) ) ) ) AS distance
FROM tt_address

我对 TYPO3 8.7 的尝试:

$statement = $queryBuilder
->select(
    'tt_address.*',
    '( 6371.41 * acos( cos( radians(' . $queryBuilder->createNamedParameter($lat, \PDO::PARAM_STR) . ') ) * cos( radians(tt_address.tx_geosearch_lat ) ) * cos( radians(tt_address.tx_geosearch_lng ) - radians(' . $queryBuilder->createNamedParameter($lng, \PDO::PARAM_STR) . ') ) + sin( radians(' . $queryBuilder->createNamedParameter($lat, \PDO::PARAM_STR) . ') ) * sin( radians(tt_address.tx_geosearch_lat ) ) ) ) AS distance'
    )
->from('tt_address')

这会导致完全损坏的 SQL 查询:

SELECT `tt_address`.*,
`( 6371`.`41 * acos( cos( radians(48.0818583) ) * cos( radians(tt_address`.`tx_geosearch_lat ) ) * cos( radians(tt_address`.`tx_geosearch_lng ) - radians(11.9879884) ) + sin( radians(48.0818583) ) * sin( radians(tt_address`.`tx_geosearch_lat ) ) ) )` AS `distance`
FROM `tt_address`

关于如何解决这个问题的任何指示?

我不确定查询生成器是否可以做到这一点。不过我会通过连接来做到这一点:

$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
$row = $connection->executeQuery('select * from ' . $tableName . ' where uid = 1')->fetchAll();

您可以使用 addSelectLiteral() 进行查询:

$statement = $queryBuilder
->select(
    'tt_address.*'
)
->addSelectLiteral('
    6371.41 * acos(
        cos(
            radians(' . $queryBuilder->createNamedParameter($lat, \PDO::PARAM_STR) . ')
        ) * cos( 
            radians(tt_address.tx_geosearch_lat) 
        ) * cos( 
            radians(tt_address.tx_geosearch_lng) - radians(' . $queryBuilder->createNamedParameter($lng, \PDO::PARAM_STR) . ')
        ) + sin(
            radians(' . $queryBuilder->createNamedParameter($lat, \PDO::PARAM_STR) . ')
        ) * sin(
            radians(tt_address.tx_geosearch_lat)
        )
    ) AS distance
')
->from('tt_address')