用于在矩形中获取地理空间点st_with的DQL查询

时间:2016-03-20 14:16:47

标签: mysql symfony geospatial dql

我想编写DQL查询来选择地图中矩形内的所有点。矩形由左上角和右下角定义。有关详细信息,请参阅此link

为此,我写了这个DQL查询:

$qb = $this->em->createQueryBuilder()
    ->select("m")
    ->from($this->getEntityClassName(), "m")
    ->where("ST_Within(m.geom, envelope(linestring(:topleft, :bottomright)))")
    ->setParameter(":topleft", $topleft)
    ->setParameter(":bottomright", $bottomright)
    ->orderBy("m.date", "DESC");

我得到的错误是:

[Syntax Error] line 0, col 110: Error: Expected =, <, <=, <>, >, >=, !=, got 'ORDER'

为了您的信息,我正在查询的实体的geom属性定义如下:

/**
 * @var point $geom
 * @ORM\Column(type="point", nullable=true)
 */
protected $geom;  

为了您的信息,SQL查询工作正常。它如下:

SELECT *FROM MotorsAds WHERE
st_within(point(lng, lat),         
envelope(linestring(point(10.090792984008772,36.83717099338201 ), 
point(10.310519546508772,36.749467295867646 ))))

我用于DQL数字函数的库(例如我们st_within)是creof/doctrine2-spatial

准备采取任何建议来解决这个问题。

谢谢,

问题更新

我尝试了你提出的解决方案如下:

    $qb = $this->em->createQueryBuilder()
        ->select("m")
        ->from($this->getEntityClassName(), "m")
        ->where(
            $this->em->createQueryBuilder()->expr()->eq(
                    "ST_Within(m.geom, envelope(linestring(:topleft, :bottomright)))",
                    $this->em->createQueryBuilder()->expr()->literal(true)
                )
        )
        ->setParameter(":topleft", $topleft)
        ->setParameter(":bottomright", $bottomright)
        ->orderBy("m.date", "DESC");

所以,我得到的错误是:

执行&#39; SELECT COUNT(*)AS dctrn_count FROM时发生异常(SELECT DISTINCT id_0 FROM(SELECT m0_.id AS id_0,m0_.price AS price_1,m0_.year AS year_2,m0_。 km AS km_3,m0_.slug AS slug_4,m0_.title AS title_5,m0_.description AS description_6,m0_.address AS address_7,m0_.isPublished AS isPublished_8,m0_.delegation AS delegation_9,m0_.lat AS lat_10,m0_.lng AS lng_11,m0_.date AS date_12,m0_.count AS count_13,AsBinary(m0_.geom)AS geom_14 FROM MotorsAds m0_ WHERE ST_Within(m0_.geom,Envelope(LineString(?,?)))= 1 ORDER BY m0_.date DESC )dctrn_result)dctrn_table&#39;使用参数[{},{}]:

SQLSTATE [22007]:无效的日期时间格式:1367非法非几何&#39;&#39; 10.090792984009 36.837170993382&#39;&#39;解析期间找到的值

2 个答案:

答案 0 :(得分:2)

试试这个:

$qb = $this->em->createQueryBuilder()
    ->select("m")
    ->from($this->getEntityClassName(), "m")
    ->where(
        $queryBuilder->expr()->eq(
                "ST_Within(m.geom, envelope(linestring(:topleft, :bottomright)))",
                $queryBuilder->expr()->literal(true)
            )
    )
    ->setParameter(":topleft", $topleft)
    ->setParameter(":bottomright", $bottomright)
    ->orderBy("m.date", "DESC");

我是从文档中提取的:https://github.com/creof/doctrine2-spatial/blob/master/doc/index.md

答案 1 :(得分:1)

下面的解决方案可以防止您将PHP点转换为DQL查询点的问题。

    $qb = $this->em->createQueryBuilder()
        ->select("m")
        ->from($this->getEntityClassName(), "m")
        ->where(
            $this->em->createQueryBuilder()->expr()->eq(
                    sprintf("ST_Within(m.geom, envelope(linestring(point(:topleftX,:topleftY), point(:bottomrightX,:bottomrightY ))))"),
                    $this->em->createQueryBuilder()->expr()->literal(true)
                )
        )
        ->setParameter(":topleftX", $topleftX)
        ->setParameter(":topleftY", $topleftY)
        ->setParameter(":bottomrightX", $bottomrightX)
        ->setParameter(":bottomrightY", $bottomrightY)
        ->orderBy("m.date", "DESC");