Laravel + Doctrine - 到ST_DISTANCE最近的地方

时间:2016-10-25 13:32:15

标签: php mysql laravel doctrine-orm

我想找到地区与我的观点之间的最小距离。我搜索它并找到ST_CONTAIN,但是当我使用这个函数时,我收到一个错误:

Expected known function, got 'ST_Distance'

这是我的代码:

public function getNearestPlace(Point $point)
{
    $neighborhoods = $this->_em
        ->createQueryBuilder()
        ->select('Region')
        ->from($this->entityClass, 'Region')
        ->where(
            '
                MIN (ST_Distance(
                    Region.regionMapCenter, 
                    GeomFromText(\'POINT(' . $point . ')\')
                ) > 0
            '
        )
        ->getQuery()
        ->execute();
    return $neighborhoods;
}

如何解决?

1 个答案:

答案 0 :(得分:0)

Doctrine附带some functions out-of-the-box但距离函数不在其中。您可以定义自己的功能,这可能对您的情况有用。可以在in chapter 14.5.5. Adding your own functions to the DQL language

找到相关文档

我还发现a nice blog post有关如何向应用程序添加距离功能的问题。也许这篇博文也会有所帮助。博客文章中的距离函数如下所示:

<?php
namespace Wantlet\ORM;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;

/**
 * DQL function for calculating distances between two points
 *
 * Example: DISTANCE(foo.point, POINT_STR(:param))
 */
class Distance extends FunctionNode {
    private $firstArg;
    private $secondArg;

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) {
        //Need to do this hacky linestring length thing because
        //despite what MySQL manual claims, DISTANCE isn't actually implemented...
        return 'GLength(LineString(' .
               $this->firstArg->dispatch($sqlWalker) .
               ', ' .
               $this->secondArg->dispatch($sqlWalker) .
           '))';
    }

    public function parse(\Doctrine\ORM\Query\Parser $parser) {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->firstArg = $parser->ArithmeticPrimary();
        $parser->match(Lexer::T_COMMA);
        $this->secondArg = $parser->ArithmeticPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }
}