2013-10-31 39 views
1

我在啓用PostGIS的Postgre數據庫上使用Symfony和Doctrine2。我有兩個表 - 財產和鄰里有以下結構:編寫Doctrine2查詢以加入兩個空間表

class Property { 

/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\Column(type="string") 
*/ 
protected $address; 

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

class Neighborhood { 

/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $gid; 

/** 
* @ORM\Column(type="string") 
*/ 
protected $name; 

/** 
* @ORM\Column(type="string") 
*/  
protected $description; 

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

在pgAdminIII,我可以寫下面的查詢工作正常:

SELECT address, neighborhood.name 
FROM property 
JOIN neighborhood 
ON ST_Contains(neighborhood.geom, property.geom) 

我怎麼能寫這篇文章DQL?我理解連接的基礎知識併爲Doctrine2映射添加註釋,但我不確定如何執行連接,因爲兩個字段不相等。我需要使用ST_Contains函數來創建連接。 我正在使用djlambert/doctrine2-spatial束進行空間數據類型和映射。我能夠單獨查詢每個表,並在每個表上創建地圖,但不知道如何選擇給定鄰域中的所有屬性。

回答

3

我想到了這一點,並希望發佈,以防其他人有類似的問題。我需要使ST_Contains成爲條件的一部分,而不僅僅是函數。

使用createQueryBuilder,這裏是語法:

$qb = $this->createQueryBuilder('n')    
      ->select("p.address as address, n.ntaname, ST_AsText(p.geom) as function")     
      ->join('Bundle\Entity\Property', 'p', 'WITH', 'ST_Contains(n.geom4326,p.geom)=true');   


    return $qb->getQuery() 
        ->getResult(); 
0

這不是一個真正的答案,而只是一個對@喬治的代碼註釋。

對於我來說,使用creof/doctrine2-spatial:0.0.1symfony/symfony:2.3.*,成爲架構與php app/console doctrine:schema:validate驗證,這樣低的情況下所需的註解:

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

隨着資本的情況下,我不斷收到此錯誤信息:

[Doctrine\DBALDBALException]                         
    Unknown column type "Point" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). [...] 
+1

在我的配置文件中,我有點映射到點 'mapping_types:point:Point' – George