简体   繁体   中英

Typo3 LTS9 Doctrine: How to use mysql @-Variables?

What's the right way to execute the following typo3 SELECT with mysql @-variables:

$dbConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($extensionTable);
$queryBuilder = $dbConnection->createQueryBuilder();

$query = $queryBuilder
->select('extensionTable.*',
'@dist:=ACOS(ROUND(SIN(RADIANS(geo.coord_lat)) * SIN(RADIANS(52.5234743092278))
   + COS(RADIANS(geo.coord_lat)) * COS(RADIANS(52.5234743092278))
   * COS(RADIANS(geo.coord_lon)      - RADIANS(13.4122033308763)),10)) * 6380 AS distance',
'@distgrp:=CASE WHEN @dist <10 THEN 1 WHEN @dist<25 THEN 2 ELSE truncate(((@dist-25)/25)+3, 0) END AS dist_group')
->from('extensionTable  LEFT JOIN geoTable AS geo ON geo.zip=extensionTable.zip')
->where('pid IN (15,16)');

It's not working, typo3 is inserting a ' before the @dist var. The bad result is:

SELECT 'extensionTable'.*, '@dist:=... ' AS 'distance';

Error : An exception occurred while executing... You have an error in your SQL syntax;

Related topic (solution doesn't work with $queryBuilder->select() ): How to use mysql variables in doctrine

I hope someone can help me. :-) (Or do I have to use a direct php mysqli() query.)

You can prevent escaping with a selectLiteral .

$queryBuilder
  ->select(...)
  ->addSelectLiteral('@dist:=COS(RADIANS(90))')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM