該庫允許您使用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許可發布。