Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Postgresql For Doctrine Laravel Package

martin-georgiev/postgresql-for-doctrine

Adds PostgreSQL-specific power to Doctrine DBAL/ORM: rich native types (jsonb, arrays, ranges, network, geometric, etc.) plus DQL functions/operators for JSON and array querying. Supports PostgreSQL 9.4+ and PHP 8.2+.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • PostgreSQL-Centric ORM Enhancement: The package is a perfect fit for Laravel applications leveraging PostgreSQL, as it bridges Doctrine’s limitations with PostgreSQL’s advanced features (e.g., JSONB, PostGIS, arrays, ranges). Laravel’s Eloquent ORM is built on Doctrine DBAL, making this a direct upgrade path for teams needing PostgreSQL-specific functionality.
  • Modular Design: The package’s type-first architecture (e.g., Jsonb, Geometry, Range) aligns with Laravel’s entity-driven model. Each type is self-contained, reducing coupling and enabling selective adoption (e.g., use only Jsonb or PostGIS without migrating all types).
  • DQL Function Support: The ~340 DQL functions (e.g., ST_Distance, to_tsvector) enable native PostgreSQL operations in queries, avoiding raw SQL and reducing vendor lock-in. This is critical for Laravel apps with complex spatial/text search logic.

Integration Feasibility

  • Laravel Compatibility: The package includes a dedicated Laravel integration guide, covering:
    • Service Provider Registration: Auto-registers types via Laravel’s registerTypes() in Doctrine\DBAL\Types\Type.
    • Eloquent Integration: Works seamlessly with Eloquent’s Attribute/Cast system (e.g., #[Attribute] for Jsonb columns).
    • Query Builder: Supports PostgreSQL functions in Laravel’s query builder (e.g., DB::select("SELECT ST_Distance(..., ...)")).
  • PostGIS/Extensions: Requires PostgreSQL extensions (e.g., PostGIS, pg_trgm, pgvector) to be enabled. Laravel’s .env can manage this via DB_CONNECTION=pgsql + extension setup in migrations.
  • Migration Path: Existing apps using raw SQL or custom Doctrine types can gradually adopt this package (e.g., start with Jsonb, then add Geometry).

Technical Risk

  • Type Conversion Nuances: Breaking changes in v3.0 (e.g., stricter Jsonb parsing) may require manual type casting in Laravel models. Mitigation: Use Doctrine’s Platform abstraction to handle platform-specific conversions.
  • Performance Overhead: Complex types (e.g., GeometryArray) may introduce serialization overhead. Benchmark with PostGIS vs. raw SQL for spatial queries.
  • Extension Dependencies: Missing extensions (e.g., pgvector) will cause runtime errors. Solution: Document required extensions in Laravel’s README and use migration hooks to enable them.
  • Laravel-Specific Quirks:
    • Eloquent Hydration: Ensure Jsonb/Array types hydrate correctly in Eloquent’s hydrate().
    • Cache Invalidation: PostgreSQL functions (e.g., ST_Distance) may bypass Laravel’s cache. Use query tags or cache versioning.

Key Questions

  1. Adoption Scope:
    • Will the team adopt all types (e.g., PostGIS, Ltree) or selectively (e.g., only Jsonb)?
    • Impact: Full adoption requires schema migrations for new types; selective adoption is lower risk.
  2. PostgreSQL Version:
    • Is the team using PostgreSQL 9.4+ (minimum supported) or a newer version (e.g., 15+)?
    • Impact: Newer versions may support additional functions (e.g., pgvector in 12+).
  3. Testing Strategy:
    • How will integration tests (Docker-based) be incorporated into Laravel’s CI?
    • Impact: Requires Docker support in CI (e.g., GitHub Actions with services: postgres).
  4. Fallback Strategy:
    • What’s the plan if a type (e.g., Geometry) fails to hydrate?
    • Impact: Need graceful degradation (e.g., log warnings, use raw SQL fallback).
  5. Long-Term Maintenance:
    • Who will handle upgrades (e.g., v3.0 → v4.0) and PostgreSQL version compatibility?
    • Impact: Laravel’s Doctrine integration may need updates if the package evolves.

Integration Approach

Stack Fit

  • Laravel + Doctrine DBAL: The package is designed for Laravel via its integration guide, covering:
    • Service Provider: Auto-registers types in Laravel’s bootstrap.
    • Eloquent: Works with #[Attribute] and Cast traits.
    • Query Builder: Supports PostgreSQL functions in DB::raw().
  • PostgreSQL Extensions: Required extensions (e.g., PostGIS, pg_trgm) must be enabled. Laravel’s .env can configure this:
    DB_CONNECTION=pgsql
    DB_POSTGRES_EXTENSIONS=postgis,pg_trgm
    
  • Alternative to Raw SQL: Replaces custom SQL for PostgreSQL-specific operations (e.g., ST_Distance), improving maintainability and type safety.

Migration Path

  1. Phase 1: Type Registration
    • Add the package to composer.json:
      composer require martin-georgiev/postgresql-for-doctrine
      
    • Publish the Laravel service provider (if not auto-discovered):
      // config/app.php
      'providers' => [
          MartinGeorgiev\Doctrine\Laravel\PostgresServiceProvider::class,
      ],
      
  2. Phase 2: Schema Migration
    • Update existing tables to use new types (e.g., jsonbJsonb):
      Schema::table('posts', function (Blueprint $table) {
          $table->jsonb('metadata')->change(); // If using Laravel migrations
      });
      
    • For new tables, use the package’s types directly:
      use MartinGeorgiev\Doctrine\DBAL\Type as PostgresType;
      
      Schema::create('geocoded_places', function (Blueprint $table) {
          $table->geometry('location', 4326); // PostGIS type
      });
      
  3. Phase 3: Model Integration
    • Update Eloquent models to use attributes or casts:
      use MartinGeorgiev\Doctrine\DBAL\Type as PostgresType;
      
      class Post extends Model {
          protected $casts = [
              'tags' => PostgresType::TEXT_ARRAY,
              'coordinates' => PostgresType::POINT,
          ];
      }
      
    • For complex types (e.g., Jsonb), use accessors:
      public function getMetadataAttribute($value) {
          return json_decode($value, true); // Or use package's JsonbArray
      }
      
  4. Phase 4: Query Migration
    • Replace raw SQL with DQL functions:
      // Before (raw SQL)
      DB::select("SELECT ST_Distance(..., ...) FROM ...");
      
      // After (DQL)
      $query = Post::query()
          ->selectRaw('ST_Distance(location, ?) as distance', [$point])
          ->get();
      
    • Use package-provided functions in Laravel’s query builder:
      use MartinGeorgiev\Doctrine\Laravel\Query\PostgresFunctions;
      
      $results = Post::where(PostgresFunctions::ST_Distance('location', $point), '<', 1000)
          ->get();
      

Compatibility

  • Doctrine DBAL: Works with Laravel 9+ (Doctrine DBAL 3.x).
  • PostgreSQL: Requires 9.4+ (for JSONB, arrays, ranges). Tested up to 18+.
  • PHP: Requires 8.2+ (due to named arguments, attributes).
  • Extensions: Optional but recommended:
    • PostGIS (for geometry/geography types).
    • pg_trgm (for trigram similarity).
    • pgvector (for vector search).

Sequencing

  1. Start with Non-Breaking Types:
    • Begin with Jsonb, Array, or UUID (low risk).
  2. Add Complex Types Gradually:
    • Move to Geometry, Range, or Ltree after validating core functionality.
  3. Test in Staging:
    • Use the Docker-based integration tests to verify PostgreSQL-specific behavior.
  4. Monitor Performance:
    • Compare query speeds between raw SQL and DQL functions (e.g., ST_Distance).

Operational Impact

Maintenance

  • Dependency Management:
    • The package is MIT-licensed and actively maintained (last release: **2026-07-0
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
bugban/symfony
beyonder-capi/workflow-extensions-bundle
beyonder-capi/job-queue-bundle
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin