Book Image

Extending Symfony2 Web Application Framework

By : Sebastien Armand
Book Image

Extending Symfony2 Web Application Framework

By: Sebastien Armand

Overview of this book

Table of Contents (13 chapters)

Custom DQL functions


Doctrine can be adapted to many different database vendors such as MySQL, PostgreSQL, and others. To achieve this and still be able to take advantage of the specifics of each underlying platform, Doctrine is designed in such a way that it is easy to define your own custom SQL functions.

We will take advantage of this for our geolocation. In the first chapter, we decided that the home page would only display events within 25 kilometers (which roughly translates to 0.3 in terms of latitude and longitude). To do so, we defined a box of coordinates around a given point and then used it in the SQL code.

However, an actual distance between two points (in a Cartesian plan) is calculated by the following formula:

The preceding formula can be translated to the following SQL query: SQRT (POW(lat_1 - lat_2, 2) + POW(long_1 - long_2, 2) ).

This is correct; however, it is a bit tedious to write, so we'll take advantage of Doctrine's ability to define your own SQL functions and define...