Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Query Builder ACOS function cannot be found

I want to calculate distance in my custom repository class. The problem is that Doctrine throws exception while processing ACOS function:

[Syntax Error] line 0, col 70: Error: Expected known function, got 'ACOS'

Here is the query:

public function findLocation($latitude, $longitude)
{
    $em = $this->getEntityManager();
    return $em->createQueryBuilder()
                    ->select('((ACOS(SIN('.$latitude.' * PI() / 180) * SIN(p.latitude * PI() / 180) + COS('.$latitude.' * PI() / 180) * COS(p.latitude * PI() / 180) * COS(('.$longitude.' – p.longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance')
                    ->from('StrictPlaceBundle:Poi', 'p')
                    ->add('orderBy', 's.distance ASC')
                    ->getQuery()->getResult();    
}

What can be wrong?

like image 491
Wojciech Jasiński Avatar asked Jun 14 '12 07:06

Wojciech Jasiński


1 Answers

ACOS is not supported by DQL. You could add it yourself of course (see Adding your own functions to the DQL language).

Also, MySQL implementations of trigonometric functions you need are part of DoctrineExtensions:

  • the test
  • functions
like image 149
Jakub Zalas Avatar answered Sep 25 '22 02:09

Jakub Zalas