【问题标题】:Write a Doctrine2 query to join two spatial tables编写一个 Doctrine2 查询来连接两个空间表
【发布时间】:2013-10-31 02:28:48
【问题描述】:

我在启用 PostGIS 的 Postgre 数据库上使用 Symfony 和 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 / dictionary2-spatial 包进行空间数据类型和映射。我能够单独查询每个表并在每个表上创建了地图,但不确定如何选择给定社区中的所有属性。

【问题讨论】:

    标签: symfony join doctrine-orm postgis spatial


    【解决方案1】:

    我想通了,想发帖以防其他人有类似的问题。我需要使 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();
    

    【讨论】:

      【解决方案2】:

      这不是一个真正的答案,而只是对@George 代码的评论。

      对我来说,使用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(). [...]
      

      【讨论】:

      • 在我的配置文件中,我将点映射到点 mapping_types: point: Point
      猜你喜欢
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 2016-10-16
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      相关资源
      最近更新 更多