我想找到区域的点和我的点之间的最小距离。我搜索它并找到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;
}


怎么解决呢?

最佳答案

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

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

<?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);
    }
}

关于php - Laravel +学说-通过ST_DISTANCE到达最近的地方,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40241401/

10-10 00:47