【问题标题】:config jsor library to make symfony 2/doctrine 2 work with postgis extension配置 jsor 库以使 symfony 2/doctrine 2 与 postgis 扩展一起工作
【发布时间】:2018-01-25 23:50:26
【问题描述】:

使用 Symfony 2.8.24 和 PostGIS

我需要THE TITLE。我在 github 中找到了this,但问题是那里的配置步骤对我没有多大帮助。我不知道是不是因为我不能使用composer online(代理)来安装它作为第一步状态。

IRDK 从哪里开始,例如,我在the files I am using 的任何地方都看不到路径'Jsor\Doctrine\PostGIS\Types\GeographyType',所以我不知道从哪里复制它们以使用它们,或者它可能与它无关。

我知道这要求很多,但是谁能带我完成它或指出我如何从一开始就将这个库包含在 symfony2.8 中,我已经使用 Symfony 有一段时间了(不是 postgis)并且仍然有包括这种第三方库真的很难,所以我们在这里谈论婴儿步骤。谢谢

【问题讨论】:

    标签: symfony doctrine-orm postgis


    【解决方案1】:

    确保您的 postgresql 服务器上安装了 postgis 扩展。

    通过 composer 安装包。

    composer require jsor/doctrine-postgis
    

    将类型添加到教义捆绑的配置中。

    例子:

    # 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]
    

    现在节省一些积分...

    最后在您的学说存储库中使用 postgis 类型:

    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()
      ;
    }
    

    【讨论】:

    • 这是一个非常全面的答案,但是,您可能错过了我不能在线使用 composer 的非常重要的部分,我认为这会阻止我使用composer require jsor/doctrine-postgis,但我相信这会对我有所帮助将来当我解决我目前的困境时
    • 我只是让它工作。我想是的,因为我在尝试 (according to this) doctrine:mapping:import --filter=tname XBundle yml: Unknown database type geometry requested, Doctrine\DBAL\Platforms\PostgreSQL92Platform may not support it. 时没有收到之前的错误,这就是促使我首先安装 jsor 的原因。但是当我尝试第二步时doctrine:mapping:convert annotation ./src我得到[Doctrine\Common\Persistence\Mapping\MappingException] Invalid mapping file FILE.orm.yml' for class <classname>
    • 你不会碰巧知道这件事吧?不过 1up 4 你的答案
    • 别再介意了,我不敢相信发生了什么。 Symfony 以某种方式认为可以在名称中使用 . 命名生成的文件,就像在 SCHEMA.TABLE.orm.yml 中一样。 ASA 我在yml 文件的命名空间中更改了它,瞧!再次感谢您提供如此详尽的答案,这将对我将来有很大帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    相关资源
    最近更新 更多