Home

Awesome

doctrine-dbal-postgresql

Latest Stable Version Total Downloads Latest Unstable Version

This component allows you to manage some native PostgreSQL data types, operators and functions with the Doctrine DBAL component.

Usage

Add to composer.json

php composer.phar require opsway/doctrine-dbal-postgresql ~0.8

To use the new types you should register them using the Custom Mapping Types feature.

To use the new functions you should register them using the DQL User Defined Functions feature.

Custom Types

Custom DQL functions

Custom DQL function usage

For an example the CONTAINS function requires your table column in your database to be of the type jsonb. Otherwise PostgreSQL will not recognize the operator needed to perform this action. (@>)

Example query:

$result = $this->em->createQuery(
    'SELECT l FROM Foo\Bar\Baz l WHERE CONTAINS(l.metaData, :value) = true')
    ->setParameter('value', json_encode(['foo'=>'bar']))
    ->getResult();

Setting the column type to jsonb.

/**
 * @var array
 *
 * @ORM\Column(type="json", nullable=true, options={"jsonb": true})
 */
private $metaData;

Note: If you want to use these DQL functions on an existing json field, you will have to alter its column type (running make:migration after adding options={"jsonb": true} will not be enough). This migration is an example of how you can do it:

<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class VersionXXX extends AbstractMigration
{
    public function up(Schema $schema): void
    {
        $this->addSql('ALTER TABLE "user" ALTER COLUMN roles SET DATA TYPE jsonb');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE "user" ALTER COLUMN roles SET DATA TYPE json');
    }
}
Custom NamePostgreSqlUsage in DQLResult in SQL
CONTAINS@>CONTAINS(field, :param)(field @> '{value}')
CONTAINED<@CONTAINED(field, :param)(field <@ '{value}')
GET_JSON_FIELD->>GET_JSON_FIELD(field, 'json_field')(table_field->>'json_field')
GET_JSON_FIELD_BY_KEY->GET_JSON_FIELD_BY_KEY(field, 'json_field')(table_field->'json_field')
GET_JSON_OBJECT#>GET_JSON_OBJECT(field, 'json_field')(table_field#>'json_field')
GET_JSON_OBJECT_TEXT#>>GET_JSON_OBJECT_TEXT(field, 'json_field')(table_field#>>'json_field')
ANY_OPANYANY_OP(field)ANY(field)
ALL_OPALLALL_OP(field)ALL(field)
ARRARRAYARR(field)ARRAY[field]
ARR_AGGREGATEarray_aggARR_AGGREGATE(field)array_agg(field)
ARR_APPENDarray_appendARR_APPEND(field, :param)array_append(field, param)
ARR_REPLACEarray_replaceARR_REPLACE(field, :param1, :param2)array_replace(field, p1, p2)
REGEXP_REPLACEregexp_replaceREGEXP_REPLACE(field, :param1, :param2)regexp_replace(field, p1, p2)
ARR_REMOVEarray_removeARR_REMOVE(field, :param)array_remove(field, param)
ARR_CONTAINS&&ARR_CONTAINS(field, :param)(field && param)
TO_TSQUERYto_tsqueryTO_TSQUERY(:param)to_tsquery('param')
TO_TSVECTORto_tsvectorTO_TSVECTOR(field)to_tsvector(field)
TS_MATCH_OP@@TS_MATCH_OP(expr1, expr2)expr1 @@ expr2
TS_CONCAT_OP
UNNESTUNNESTUNNEST(field)UNNEST(field)
JSON_AGGjson_aggJSON_AGG(expression)json_agg(expression)
JSONB_ARRAY_ELEM_TEXTjsonb_array_elements_textJSONB_ARRAY_ELEM_TEXT(field, 'json_field')jsonb_array_elements_text(field)