Created
July 20, 2011 09:38
-
-
Save yoye/1094671 to your computer and use it in GitHub Desktop.
DQL fucntion : calculate distances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\GeoBundle\AST\Functions; | |
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | |
use Doctrine\ORM\Query\Parser; | |
use Doctrine\ORM\Query\SqlWalker; | |
use Doctrine\ORM\Query\Lexer; | |
class Geo extends FunctionNode | |
{ | |
/** | |
* @var \Doctrine\ORM\Query\AST\ComparisonExpression | |
*/ | |
private $latitude; | |
/** | |
* @var \Doctrine\ORM\Query\AST\ComparisonExpression | |
*/ | |
private $longitude; | |
/** | |
* Parse DQL Function | |
* | |
* @param Parser $parser | |
*/ | |
public function parse(Parser $parser) | |
{ | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->latitude = $parser->ComparisonExpression(); | |
$parser->match(Lexer::T_COMMA); | |
$this->longitude = $parser->ComparisonExpression(); | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
/** | |
* Get SQL | |
* | |
* @param SqlWalker $sqlWalker | |
* @return string | |
*/ | |
public function getSql(SqlWalker $sqlWalker) | |
{ | |
return sprintf('((ACOS(SIN(%s * PI() / 180) * SIN(%s * PI() / 180) + COS(%s * PI() / 180) * COS(%s * PI() / 180) * COS((%s - %s) * PI() / 180)) * 180 / PI()) * 60 * %s)', | |
$this->latitude->rightExpression->dispatch($sqlWalker), | |
$this->latitude->leftExpression->dispatch($sqlWalker), | |
$this->latitude->rightExpression->dispatch($sqlWalker), | |
$this->latitude->leftExpression->dispatch($sqlWalker), | |
$this->longitude->rightExpression->dispatch($sqlWalker), | |
$this->longitude->leftExpression->dispatch($sqlWalker), | |
'1.1515 * 1.609344'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment