Enhances Doctrine with PostgreSQL-specific features and functions. Supports PostgreSQL 9.4+ and PHP 8.2+.
use Doctrine\DBAL\Types\Type;
use MartinGeorgiev\Doctrine\DBAL\Type as PostgresType;
// Register types with Doctrine
Type::addType('jsonb', "MartinGeorgiev\\Doctrine\\DBAL\\Types\\Jsonb");
Type::addType('text[]', "MartinGeorgiev\\Doctrine\\DBAL\\Types\\TextArray");
Type::addType('numrange', "MartinGeorgiev\\Doctrine\\DBAL\\Types\\NumRange");
// Use in your Doctrine entities
#[ORM\Column(type: PostgresType::JSONB)]
private array $data;
#[ORM\Column(type: PostgresType::TEXT_ARRAY)]
private array $tags;
#[ORM\Column(type: PostgresType::NUMRANGE)]
private NumericRange $priceRange;
// Use in DQL
$query = $em->createQuery('
SELECT e
FROM App\Entity\Post e
WHERE CONTAINS(e.tags, ARRAY(:tags)) = TRUE
AND JSON_GET_FIELD(e.data, :field) = :value
');- Array Types
- Integer arrays (
int[],smallint[],bigint[]) - Float arrays (
real[],double precision[]) - Text arrays (
text[]) - Boolean arrays (
bool[]) - Date arrays (
date[],timestamp[],timestamptz[]) - JSONB arrays (
jsonb[])
- Integer arrays (
- Bit String Types
- Fixed-length bit strings (
bit,bit[]) - Variable-length bit strings (
bit varying,bit varying[])
- Fixed-length bit strings (
- JSON Types
- Native JSONB support
- JSON field operations
- JSON construction and manipulation
- Network Types
- IP addresses (
inet,inet[]) - Network CIDR notation (
cidr,cidr[]) - MAC addresses (
macaddr,macaddr[],macaddr8,macaddr8[])
- IP addresses (
- Geometric Types
- Box (
box,box[]) - Circle (
circle,circle[]) - Line (
line,line[]) - Line segment (
lseg,lseg[]) - Path (
path,path[]) - Point (
point,point[]) - Polygon (
polygon,polygon[]) - PostGIS Geometry (
geometry,geometry[]) - PostGIS Geography (
geography,geography[])
- Box (
- Range Types
- Date and time ranges (
daterange,tsrange,tstzrange) - Numeric ranges (
numrange,int4range,int8range) - Multiranges (
nummultirange,int4multirange,int8multirange)
- Date and time ranges (
- Interval Types
- Time durations with
DateIntervalsupport (interval,interval[])
- Time durations with
- Text Search Types
- Full-text search document (
tsvector,tsvector[]) - Full-text search query (
tsquery,tsquery[])
- Full-text search document (
- Monetary Types
- Currency amounts (
money,money[])
- Currency amounts (
- XML Types
- Native XML document storage (
xml,xml[])
- Native XML document storage (
- Hierarchical Types
- Label-tree data (
ltree,ltree[])
- Label-tree data (
- Vector Types (requires pgvector extension)
- Fixed-dimension float vector (
vector) - Half-precision float vector (
halfvec) - Sparse vector (
sparsevec)
- Fixed-dimension float vector (
- Composite Types
- Access fields from user-defined composite types via
COMPOSITE_FIELD()function
- Access fields from user-defined composite types via
- Array Operations
- Contains (
@>) - Is contained by (
<@) - Overlaps (
&&) - Array aggregation with ordering
- Contains (
- JSON Operations
- Field access (
->,->>) - Path operations (
#>,#>>) - JSON containment and existence operators
- Field access (
- Range Operations
- Containment checks (in PHP value objects and for DQL queries with
@>and<@) - Overlaps (
&&)
- Containment checks (in PHP value objects and for DQL queries with
- PostGIS Spatial Operations
- Bounding box relationships (
<<,>>,&<,&>,|&>,&<|,<<|,|>>) - Spatial containment (
@,~) - Distance calculations (
<->,<#>,<<->>,<<#>>,|=|) - N-dimensional operations (
&&&)
- Bounding box relationships (
- Text Search
- Full text search (
to_tsvector,to_tsquery) - Pattern matching (
ilike,similar to) - Regular expressions
- Trigram similarity (
similarity,word_similarity,strict_word_similarity) (requires pg_trgm extension)
- Full text search (
- Array Functions
- Array aggregation (
array_agg) - Array manipulation (
array_append,array_prepend,array_remove,array_replace,array_shuffle) - Array dimensions and length
- Array aggregation (
- JSON Functions
- JSON construction (
json_build_object,jsonb_build_object) - JSON manipulation and transformation
- JSON construction (
- Date Functions
- Aggregate Functions
- Aggregation with ordering and distinct (
array_agg,json_agg,jsonb_agg) - Special aggregates (
any_value,xmlagg)
- Aggregation with ordering and distinct (
- Mathematical/Arithmetic Functions
- Trigonometric functions (
sin,cos,tan,asin,acos,atan, degree variants) - Hyperbolic functions (
sinh,cosh,tanh,asinh,acosh,atanh) - Number theory functions (
gcd,lcm,factorial,div) - Statistical functions (
erf,erfc,random_normal)
- Trigonometric functions (
- Range Functions
- Data Type Formatting Functions
- Vector Distance Functions
Full documentation:
- Available Types
- Value Objects for Range Types
- PostgreSQL ltree Types
- Available Functions and Operators - Overview and cross-references
- Common Use Cases and Examples
- Spatial Types
- Geometry Arrays
composer require martin-georgiev/postgresql-for-doctrineSee our Common Use Cases and Examples for detailed code samples.
composer run-unit-testsWe also provide integration tests that run against a real PostgreSQL database with PostGIS:
# Start PostgreSQL with PostGIS using Docker Compose
docker compose up -d
# Run integration tests
composer run-integration-tests
# Stop PostgreSQL
docker compose down -vSee tests/Integration/README.md for more details.
If you find this package useful for your projects, please consider sponsoring the development via GitHub Sponsors. Your support helps maintain this package, create new features, and improve documentation.
Benefits of sponsoring:
- Priority support for issues and feature requests
- Direct access to the maintainer
- Help sustain open-source development
- Star the repository
- Report issues
- Contribute with code or documentation
- Share the project with others
This package is licensed under the MIT License. See the LICENSE file for details.
