• Stars
    star
    146
  • Rank 251,969 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add JSON query support to Doctrine DBAL and DQL

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

  • 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.

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 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
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)

More Repositories

1

documents

Service stateless API for converting HTML documents to PDF
Go
25
star
2

doctrine-orm-swoole

Doctrine EntityManager Decorator for Swoole & coroutine (concurrency requests) support
PHP
13
star
3

doctrine-dbal-swoole-pgsql-driver

Doctrine DBAL Driver for Swoole Postgresql database connections
PHP
10
star
4

magento-mod-onelogin

Magento Admin - Onelogin integration (SAML)
PHP
9
star
5

magento2-module-amazonses

PHP
9
star
6

zohobooks-api

ZohoBooks PHP library
PHP
6
star
7

psr12-strict-modern-standart

Shell
6
star
8

magento2-onelogin

PHP
5
star
9

opsway-magerun-addons

Modules for n98-magerun
PHP
5
star
10

odoo-docker-extend

Allows easy docker deployment on production server
Python
4
star
11

magento-connect-packager

Automatically pack magento extension code to archive for Magento Connect 2.0
PHP
4
star
12

praefectus

Manager for worker processes (message consumers)
Go
4
star
13

m2-stack

VCL
3
star
14

chef-hipchat

Opscode Chef HipChat exception handler
Ruby
3
star
15

magento-mod-varnishgento

Magento Varnish smart integration module
PHP
3
star
16

magento-mod-rum

Real-User-Monitoring module that gather information with Boomerang and send it Google Analytics
JavaScript
3
star
17

laminas-service-manager-attributes

PHP
2
star
18

dockerfile

Extended syntax dockerfile based on Buildkit
Shell
2
star
19

react-native-clevertap-inbox

React Native wrapper for the Custom App Inbox feature of CleverTap SDK
Objective-C
2
star
20

magento-composer-core-deploy

Additional command to Composer Command Integrator
PHP
2
star
21

magento-mod-splunklog

Module for sending logs/info to Splunk by UDP
PHP
2
star
22

chef-rkhunter

rkhunter cookbook
Ruby
2
star
23

magento2-facebookpopup-module

Facebook popup. Magento 2 module
CSS
2
star
24

magento-mod-cloudfront

Magento module that creates links to CSS/JS files based on modification dates
PHP
2
star
25

nodal

TypeScript
2
star
26

magento-training

Magento training materials, articles, links for first stage
CSS
2
star
27

magento-mod-debug

Magento module for performance issues debug
PHP
2
star
28

highrise-migrate

Highrise migration script
PHP
1
star
29

magento-mod-export

PHP
1
star
30

chef-monit-ops

Ruby
1
star
31

magento-mod-adminnotifications

Magento module that disable/enable Admin notifications
PHP
1
star
32

magento-mod-mainadv

Magento module for MainAdv pixels integration
PHP
1
star
33

magento-mod-magexim

Magento export/import shell script runner
PHP
1
star
34

slim-attribute-router

PHP
1
star
35

tocat-redmine-client

TOCAT redmine client
Ruby
1
star
36

odoo-knowledge-base

1
star
37

tocat-opsdesk-platform

Accounting platform for company based on Theory Of Constraints
PHP
1
star
38

ZfcUserOnelogin

OneLogin integration to ZfcUser ZF2 module
PHP
1
star
39

clickpost-api

PHP SDK for Clickpost Indian Provider
PHP
1
star
40

magento2-blog

Magento2 Blog
1
star