我在启用了PostGIS的Postgre数据库上使用Symfony和Doctrine2。 我有两个表 - 具有以下结构的财产和社区:
class Property {
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string")
*/
protected $address;
/**
* @var Point $geom
* @ORMColumn(type="Point", nullable=true)
*/
protected $geom;
}
class Neighborhood {
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $gid;
/**
* @ORMColumn(type="string")
*/
protected $name;
/**
* @ORMColumn(type="string")
*/
protected $description;
/**
* @var Polygon $geom
*
* @ORMColumn(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-spacebundle进行空间数据类型和映射。 我能够单独查询每个表,并在每个表上创建了地图,但不确定如何选择给定社区中的所有属性。
通了这一点,并想发帖以防其他人有类似的问题。 我需要使ST_Contains成为条件的一部分,而不仅仅是函数。
使用 createQueryBuilder,语法如下:
$qb = $this->createQueryBuilder('n')
->select("p.address as address, n.ntaname, ST_AsText(p.geom) as function")
->join('BundleEntityProperty', 'p', 'WITH', 'ST_Contains(n.geom4326,p.geom)=true');
return $qb->getQuery()
->getResult();
一个真正的答案,而只是对@George代码的评论。
对我来说,使用 creof/doctrine2-spatial:0.0.1
和 symfony/symfony:2.3.*
时,注释需要小写才能用 php app/console doctrine:schema:validate
验证模式,如下所示:
/**
* @var Point $geom
* @ORMColumn(type="point", nullable=true)
*/
protected $geom;
对于大写字母,我不断收到此错误消息:
[DoctrineDBALDBALException]
Unknown column type "Point" requested. Any Doctrine type that you use has to be registered with DoctrineDBALTypesType::addType(). [...]