2016-03-20 54 views
2

我想編寫DQL查詢來選擇地圖中矩形內的所有點。矩形由左上角和右下角定義。有關更多詳細信息,請參閱此linkDQL查詢獲取地理空間點st_within一個矩形

要做到這一點,我寫了這個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"); 

所以,我得到的錯誤是:

執行'SELECT COUNT(*)AS dctrn_count FRO時發生異常M 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,包絡(線段形式(?,)))= 1個ORDER BY m0_.date DESC)dctrn_result)dctrn_table」使用參數[{},{}]:

SQLSTATE [22007]:無效的日期時間格式:1367非法非幾何''10 .090792984009 36.837170993382''在解析期間發現的值

回答

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"); 
2

嘗試t他:

$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

+0

謝謝@Miro給你回答。我更新我的問題。我可以給你評論嗎? –

+0

您提出的解決方案的問題是點的轉換....以下是我的解決方案。再次感謝@Miro –

+0

好,Amine – Miro