该库允许您使用PostGIS(PostgreSQL的空间数据库扩展程序)使用学说(ORM或DBAL)。
下表显示了该库正式支持的版本。
依赖性 | 支持版本 |
---|---|
邮政 | 3.0和3.1 |
Postgresql | 11、12和13 |
学说 | ^2.9 |
学说dbal | ^2.13和 ^3.1 |
使用作曲家安装最新版本。
composer require jsor/doctrine-postgis
查看所有可用版本的Packagist页面。
要将图书馆与学说ORM一起使用,请注册ORMSchemaEventSubscriber
事件订户。
use Jsor Doctrine PostGIS Event ORMSchemaEventSubscriber ;
$ entityManager -> getEventManager ()-> addEventSubscriber ( new ORMSchemaEventSubscriber ());
要仅与DBAL一起使用,请注册DBALSchemaEventSubscriber
EVENT订户。
use Jsor Doctrine PostGIS Event DBALSchemaEventSubscriber ;
$ connection -> getEventManager ()-> addEventSubscriber ( new DBALSchemaEventSubscriber ());
要将此库集成到一个符号项目中,请阅读专用的Symfony文档。
事件订户注册后,可以在属性映射中使用列类型的几何geometry
和geography
(请阅读PostGIS文档以了解这两种类型之间的区别)。
use Doctrine ORM Mapping as ORM ;
use Jsor Doctrine PostGIS Types PostGISType ;
#[ ORM Entity ]
class MyEntity
{
#[ ORM Column (type: PostGISType :: GEOMETRY )]
private string $ geometry ;
#[ ORM Column (type: PostGISType :: GEOGRAPHY )]
private string $ geography ;
}
有2个配置几何形状的选项。
geometry_type
这定义了几何类型,例如POINT
, LINESTRING
等。如果省略此选项,则使用通用类型的GEOMETRY
。srid
这定义了几何形状的空间参考系统标识符(SRID)。 use Doctrine ORM Mapping as ORM ;
use Jsor Doctrine PostGIS Types PostGISType ;
#[ ORM Entity ]
class MyEntity
{
#[ ORM Column (
type: PostGISType :: GEOMETRY ,
options: [ ' geometry_type ' => ' POINT ' ],
)]
public string $ point ;
#[ ORM Column (
type: PostGISType :: GEOMETRY ,
options: [ ' geometry_type ' => ' POINTZM ' ],
)]
public string $ point4D ;
#[ ORM Column (
type: PostGISType :: GEOMETRY ,
options: [ ' geometry_type ' => ' POINT ' , ' srid ' => 3785 ],
)]
public string $ pointWithSRID ;
public function __construct (
string $ point ,
string $ point4D ,
string $ pointWithSRID ,
) {
$ this -> point = $ point ;
$ this -> point4D = $ point4D ;
$ this -> pointWithSRID = $ pointWithSRID ;
}
}
为属性提供的值必须为WKT格式。请注意,从数据库返回的值可能与您设置的值不同。该库使用ST_ASEWKT保留尽可能多的信息(例如SRID)。在PostGIS文档中阅读更多内容。
$ entity = new MyEntity (
point: ' POINT(-122.0845187 37.4220761) ' ,
point4D: ' POINT(1 2 3 4) ' ,
pointWithSRID: ' SRID=3785;POINT(-122.0845187 37.4220761) ' ,
);
可以通过设置spatial
标志来为几何场定义空间索引。
use Doctrine ORM Mapping as ORM ;
#[ ORM Entity ]
#[ ORM Index (
fields: [ ' pointWithSRID ' ],
flags: [ ' spatial ' ],
)]
class MyEntity
{
}
提供了对ORM架构工具和DBAL模式管理器的全面支持。
大多数PostGIS函数也可用于JsorDoctrinePostGISFunctions
namespace下的学说查询语言(DQL)。
有关所有受支持功能的完整列表,请参见功能索引。
阅读有关如何使用Symfony配置功能的专用Symfony文档。
该功能必须在DoctrineORMConfiguration
实例中注册。
$ configuration = new Doctrine ORM Configuration ();
$ configuration -> addCustomStringFunction (
' ST_Within ' ,
Jsor Doctrine PostGIS Functions ST_Within ::class
);
$ configuration -> addCustomNumericFunction (
' ST_Distance ' ,
Jsor Doctrine PostGIS Functions ST_Distance ::class
);
$ dbParams = [ /***/ ];
$ entityManager = Doctrine ORM EntityManager :: create ( $ dbParams , $ configuration );
有一个便利的配置器类,可用于一次注册所有功能。
$ configuration = new Doctrine ORM Configuration ();
Jsor Doctrine PostGIS Functions Configurator :: configure ( $ configuration );
$ dbParams = [ /***/ ];
$ entityManager = Doctrine ORM EntityManager :: create ( $ dbParams , $ configuration );
阅读有关如何用Symfony处理这些问题的专用Symfony文档。
由于Postgis可以添加一些新的模式,例如topology
, tiger
和tiger_data
,因此您可能需要将它们排除在被学说处理之外。
这可以通过配置模式资产过滤器来完成。
$ configuration = new Doctrine ORM Configuration ();
$ configuration -> setSchemaAssetsFilter ( static function ( $ assetName ): bool {
if ( $ assetName instanceof AbstractAsset ) {
$ assetName = $ assetName -> getName ();
}
return ( bool ) preg_match ( ' /^(?!tiger)(?!topology)/ ' , $ assetName );
});
$ dbParams = [ /***/ ];
$ entityManager = Doctrine ORM EntityManager :: create ( $ dbParams , $ configuration );
有时,模式工具偶然发现了无法处理的数据库类型。一个常见的例外是类似的
DoctrineDBALException: Unknown database type _text requested, DoctrineDBALPlatformsPostgreSQL100Platform may not support it.
为了解决这个问题,未知数据库类型可以映射到已知类型。
$ configuration = new Doctrine ORM Configuration ();
$ dbParams = [ /***/ ];
$ entityManager = Doctrine ORM EntityManager :: create ( $ dbParams , $ configuration );
$ entityManager -> getConnection ()-> getDatabasePlatform ()-> registerDoctrineTypeMapping ( ' _text ' , ' string ' );
注意:那么这种类型不适合用于实体映射。它只是防止了模式工具在数据库检查期间抛出的“未知数据库类型...”。
如果要在实体中使用此类型,则必须配置真实的数据库类型,例如使用PostgreSQL进行学说软件包。
包括一个简单的Docker设置,以针对不同的PostgreSQL / PostGIS组合运行测试套件。
这里的所有命令都应从项目根部运行。
首先,构建PHP容器。这必须只完成一次。
./docker/build-php.sh
通过作曲家安装依赖项。
./docker/run-php.sh composer install
接下来,启动数据库容器。
docker compose -f ./docker/docker-compose.yml up -d
在连接到特定数据库容器的PHP容器中,有许多可执行命令的快捷脚本。
脚本名称遵循模式run-<POSTGRESQL_VERSION>-<POSTGIS_VERSION>.sh
。
要使用PostGIS 3.1使用PostgreSQL 13运行测试套件,请使用脚本./docker/run-13-31.sh
。
./docker/run-13-31.sh vendor/bin/phpunit --exclude-group=postgis-3.0
请注意,我们在此处排除针对PostGIS 3.0的测试。针对PostGIS 3.0进行测试时,将3.1的测试排除。
./docker/run-13-30.sh vendor/bin/phpunit --exclude-group=postgis-3.1
版权(C)2014-2024 Jan Sorgalla。根据MIT许可发布。