PHP code example of martin-georgiev / postgresql-for-doctrine

1. Go to this page and download the library: Download martin-georgiev/postgresql-for-doctrine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

martin-georgiev / postgresql-for-doctrine example snippets


use Doctrine\DBAL\Types\Type as DoctrineType;
use Doctrine\ORM\Mapping as ORM;
use MartinGeorgiev\Doctrine\DBAL\Type;
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\NumericRange;

// Register types with Doctrine
// DBAL 4.3+ ships its own jsonb type, which, unlike this one, reads integers beyond PHP_INT_MAX as floats and writes 1.0 instead of 1
if (DoctrineType::hasType('jsonb')) {
    DoctrineType::overrideType('jsonb', "MartinGeorgiev\\Doctrine\\DBAL\\Types\\Jsonb");
} else {
    DoctrineType::addType('jsonb', "MartinGeorgiev\\Doctrine\\DBAL\\Types\\Jsonb");
}
DoctrineType::addType('text[]', "MartinGeorgiev\\Doctrine\\DBAL\\Types\\TextArray");
DoctrineType::addType('numrange', "MartinGeorgiev\\Doctrine\\DBAL\\Types\\NumRange");

// Use in your Doctrine entities
#[ORM\Column(type: Type::JSONB)]
private array $data;

#[ORM\Column(type: Type::TEXT_ARRAY)]
private array $tags;

#[ORM\Column(type: Type::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
');