2017 © Pedro Peláez
 

library doctrine-dbal-postgresql

Extensions for support Postgres in Doctrine DBAL & DQL

image

opsway/doctrine-dbal-postgresql

Extensions for support Postgres in Doctrine DBAL & DQL

  • Tuesday, July 17, 2018
  • by shandyDev
  • Repository
  • 39 Watchers
  • 66 Stars
  • 122,511 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 13 Forks
  • 5 Open issues
  • 22 Versions
  • 17 % Grown

The README.md

doctrine-dbal-postgresql

Latest Stable Version Total Downloads Latest Unstable Version, (*1)

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

Usage

Add to composer.json, (*3)

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., (*4)

To use the new functions you should register them using the DQL User Defined Functions feature., (*5)

Custom Types

  • Array Integer (integer[])
  • Array BigInt (bigint[])
  • TsVector (tsvector)

Custom DQL functions

  • CONTAINS - 'OpsWay\Doctrine\ORM\Query\AST\Functions\Contains'
  • CONTAINED - 'OpsWay\Doctrine\ORM\Query\AST\Functions\Contained'
  • GET_JSON_FIELD - 'OpsWay\Doctrine\ORM\Query\AST\Functions\GetJsonField'
  • GET_JSON_FIELD_BY_KEY - 'OpsWay\Doctrine\ORM\Query\AST\Functions\GetJsonFieldByKey'
  • GET_JSON_OBJECT - 'OpsWay\Doctrine\ORM\Query\AST\Functions\GetJsonObject'
  • GET_JSON_OBJECT_TEXT - 'OpsWay\Doctrine\ORM\Query\AST\Functions\GetJsonObjectText'
  • ANY_OP - 'OpsWay\Doctrine\ORM\Query\AST\Functions\Any'
  • ALL_OP - 'OpsWay\Doctrine\ORM\Query\AST\Functions\All'
  • ARR - 'OpsWay\Doctrine\ORM\Query\AST\Functions\Arr'
  • ARR_AGGREGATE - 'OpsWay\Doctrine\ORM\Query\AST\Functions\ArrayAggregate'
  • ARR_APPEND - 'OpsWay\Doctrine\ORM\Query\AST\Functions\ArrayAppend'
  • ARR_REPLACE - 'OpsWay\Doctrine\ORM\Query\AST\Functions\ArrayReplace'
  • REGEXP_REPLACE - 'OpsWay\Doctrine\ORM\Query\AST\Functions\RegexpReplace'
  • ARR_REMOVE - 'OpsWay\Doctrine\ORM\Query\AST\Functions\ArrayRemove'
  • ARR_CONTAINS - 'OpsWay\Doctrine\ORM\Query\AST\Functions\ArrayContains'
  • TO_TSQUERY - 'OpsWay\Doctrine\ORM\Query\AST\Functions\ToTsquery'
  • TO_TSVECTOR - 'OpsWay\Doctrine\ORM\Query\AST\Functions\ToTsvector'
  • TS_CONCAT_OP - 'OpsWay\Doctrine\ORM\Query\AST\Functions\TsConcat'
  • TS_MATCH_OP - 'OpsWay\Doctrine\ORM\Query\AST\Functions\TsMatch'
  • UNNEST - 'OpsWay\Doctrine\ORM\Query\AST\Functions\Unnest'
  • JSON_AGG - 'OpsWay\Doctrine\ORM\Query\AST\Functions\JsonAgg'
  • JSONB_ARRAY_ELEM_TEXT - 'OpsWay\Doctrine\ORM\Query\AST\Functions\JsonbArrayElementsText'

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. (@>) * Tip: Based on the function you want to use, check if there are any specific column type requirements., (*6)

Example query:, (*7)

$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., (*8)

/**
 * @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:, (*9)

<?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 Name PostgreSql Usage in DQL Result 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_OP ANY ANY_OP(field) ANY(field)
ALL_OP ALL ALL_OP(field) ALL(field)
ARR ARRAY ARR(field) ARRAY[field]
ARR_AGGREGATE array_agg ARR_AGGREGATE(field) array_agg(field)
ARR_APPEND array_append ARR_APPEND(field, :param) array_append(field, param)
ARR_REPLACE array_replace ARR_REPLACE(field, :param1, :param2) array_replace(field, p1, p2)
REGEXP_REPLACE regexp_replace REGEXP_REPLACE(field, :param1, :param2) regexp_replace(field, p1, p2)
ARR_REMOVE array_remove ARR_REMOVE(field, :param) array_remove(field, param)
ARR_CONTAINS && ARR_CONTAINS(field, :param) (field && param)
TO_TSQUERY to_tsquery TO_TSQUERY(:param) to_tsquery('param')
TO_TSVECTOR to_tsvector TO_TSVECTOR(field) to_tsvector(field)
TS_MATCH_OP @@ TS_MATCH_OP(expr1, expr2) expr1 @@ expr2
TS_CONCAT_OP | TS_CONCAT_OP(expr1, expr2, ....) | (expr1 || expr2 || ...)
UNNEST UNNEST UNNEST(field) UNNEST(field)
JSON_AGG json_agg JSON_AGG(expression) json_agg(expression)
JSONB_ARRAY_ELEM_TEXT jsonb_array_elements_text JSONB_ARRAY_ELEM_TEXT(field, 'json_field') jsonb_array_elements_text(field)

The Versions

17/07 2018

dev-master

9999999-dev

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

The Development Requires

14/03 2018

v0.8.1

0.8.1.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

The Development Requires

06/02 2018

v0.8.0

0.8.0.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

23/08 2017

v0.7.4

0.7.4.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

23/08 2017

dev-add_regexp_replace

dev-add_regexp_replace

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

07/10 2015

v0.7.3

0.7.3.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

12/08 2015

v0.7.2

0.7.2.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

07/08 2015

dev-develop

dev-develop

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

The Development Requires

05/08 2015

v0.7.1

0.7.1.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

29/07 2015

v0.7.0

0.7.0.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

23/07 2015

v0.6.3

0.6.3.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

23/07 2015

v0.6.2

0.6.2.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

08/07 2015

v0.6.1

0.6.1.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

30/06 2015

v0.6.0

0.6.0.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

20/05 2015

v0.5.1

0.5.1.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

20/05 2015

v0.5.0

0.5.0.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

17/05 2015

v0.4.1

0.4.1.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

23/04 2015

v0.4.0

0.4.0.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

21/04 2015

v0.3.0

0.3.0.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

20/04 2015

v0.2.0

0.2.0.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

05/04 2015

v0.1.2

0.1.2.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires

 

05/04 2015

v0.1.1

0.1.1.0

Extensions for support Postgres in Doctrine DBAL & DQL

  Sources   Download

MIT

The Requires