配置jsor库以使用postgis扩展使symfony 2/doctrine 2正常工作 [英] config jsor library to make symfony 2/doctrine 2 work with postgis extension

查看:65
本文介绍了配置jsor库以使用postgis扩展使symfony 2/doctrine 2正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Symfony 2.8.24和PostGIS

Working with Symfony 2.8.24 and PostGIS

我需要THE TITLE.我在github中找到了,但是问题是那里的配置步骤对我没有太大帮助.我不知道是否是因为我不能在线使用作曲家(代理)来作为第一步说明安装它.

I need to THE TITLE. I found this in github but the problem is the configuration steps there didn't help me a lot. I don't know if it is because I can't use composer online (proxy) to install it as the first step states.

IRDK从哪里开始,例如,在

IRDK where to start, for example, I don't see the path 'Jsor\Doctrine\PostGIS\Types\GeographyType' anywhere in the files I am using so I don't know where to copy them to use them, or maybe it has nothing to do with it.

我知道它要问的很多,但是任何人都可以从头到尾介绍一下我或从2开始如何将这个库包含在symfony2.8中,我现在已经使用Symfony了一段时间(不是postgis),并且仍然有包括这类第三方库,确实非常困难,所以我们在这里说些婴儿的步骤.谢谢

I know it's asking a lot but could anyone walk me through it or point me somewhere on how 2 include this library in symfony2.8 from the beginning, I have been using Symfony for a while now (not postgis) and still have a really hard time including this kind of third party libs, so we're talking baby steps here. thanx

推荐答案

确保已在postgresql服务器上安装了postgis扩展.

Ensure you have the postgis extension installed on your postgresql server.

通过composer安装软件包.

Install the package via composer.

composer require jsor/doctrine-postgis

将类型添加到doctrine-bundle的配置中.

Add the types to doctrine-bundle's configuration.

示例:

# app/config/config.yml
doctrine:
  orm:
    # [..]
    entity_managers:
      default:
        dql:
          string_functions:
            Geometry:             Jsor\Doctrine\PostGIS\Functions\Geometry
            Geography:            Jsor\Doctrine\PostGIS\Functions\Geography
            ST_Distance:          Jsor\Doctrine\PostGIS\Functions\ST_Distance
            ST_Distance_Sphere:   Jsor\Doctrine\PostGIS\Functions\ST_Distance_Sphere
            ST_DWithin:           Jsor\Doctrine\PostGIS\Functions\ST_DWithin
            ST_GeomFromText:      Jsor\Doctrine\PostGIS\Functions\ST_GeomFromText
            ST_GeographyFromText: Jsor\Doctrine\PostGIS\Functions\ST_GeographyFromText
            ST_Transform:         Jsor\Doctrine\PostGIS\Functions\ST_Transform
            ST_Point:             Jsor\Doctrine\PostGIS\Functions\ST_Point
            ST_SetSRID:           Jsor\Doctrine\PostGIS\Functions\ST_SetSRID
            ST_AsEWKT:            Jsor\Doctrine\PostGIS\Functions\ST_AsEWKT

创建实体:

namespace My\Entity;

class GeoLocation
{
    protected $latitude;

    protected $longitude;

    protected $point;

    public function __construct($latitude, $longitude)
    {
        $this->longitude = $longitude;
        $this->latitude = $latitude;
        $this->setPoint();
    }

    protected function setPoint()
    {
        $this->point = sprintf(
          'POINT(%f %f)', 
          (string)$this->longitude,
          (string)$this->latitude
        );
    }

    public function getPoint()
    {
        return $this->point;
    }

    public function getLatitude()
    {
        return (float) $this->latitude;
    }

    public function getLongitude()
    {
        return (float) $this->longitude;
    }
}

为您的实体添加映射:

My\Entity\GeoLocation:
  type: 'entity'
  indexes:
    idx_point:
      columns:
        - 'point'
      flags:
        - 'spatial'
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    longitude:
      nullable: true
      type: 'decimal'
      precision: 9
      scale: 6
    latitude:
      nullable: true
      type: 'decimal'
      precision: 8
      scale: 6
    point:
      nullable: true
      type: 'geography'
      options:
        geometry_type: 'POINT'
        srid: 4326

更新您的数据库架构:

app/console doctrine:schema:update [--force]

现在保存一些要点...

Now save some points ...

最后在您的学说资料库中使用postgis类型:

Finally use the postgis types in your doctrine repository:

public function findByDistanceFrom($latitude, $longitude, $distanceInMeters)
{
  $qb = $this->createQueryBuilder('geolocation');
  $pointFrom = 'Geography(ST_SetSRID(ST_Point(geolocation.longitude, geolocation.latitude), 4326))';
  $pointTo = 'Geography(ST_SetSRID(ST_Point(:longitude, :latitude), 4326))';
  $qb
    ->where( $qb->expr()->eq("ST_DWithin($pointFrom, $pointTo, :distance_in_meters)", $qb->expr()->literal(true) ) )
    ->setParameter('latitude', $latitude)
    ->setParameter('longitude', $longitude)
    ->setParameter('distance_in_meters', $distanceInMeters)
  ;

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

这篇关于配置jsor库以使用postgis扩展使symfony 2/doctrine 2正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆