如何使用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')

相关内容

  • 没有找到相关文章

最新更新