opsway / doctrine-dbal-postgresql
Extensions for support Postgres in Doctrine DBAL & DQL
Installs: 1 618 682
Dependents: 1
Suggesters: 0
Security: 0
Stars: 159
Watchers: 49
Forks: 37
Open Issues: 9
Requires
- php: ^8.1
- doctrine/dbal: ^4.0
- doctrine/orm: ^3.0
Requires (Dev)
- ext-pdo_sqlite: *
- lctrs/psalm-psr-container-plugin: ^1.6
- opsway/psr12-strict-coding-standard: ^1.1
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.18
- symfony/cache: ^4.4 || ^5.4 || ^6.0
- vimeo/psalm: ^5.0
- weirdan/doctrine-psalm-plugin: ^2.0
- dev-master / 3.0.x-dev
- v2.0.0
- v1.2.0
- v1.1.0
- dev-develop / 1.0.x-dev
- v1.0.0
- 1.0.0-rc1
- v0.8.1
- v0.8.0
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.1
- v0.5.0
- v0.4.1
- v0.4.0
- v0.3.0
- v0.2.0
- v0.1.2
- v0.1.1
- dev-add-infection
- dev-improve-require-versions
- dev-improve-tests
- dev-add_regexp_replace
This package is auto-updated.
Last update: 2024-11-20 13:40:53 UTC
README
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'); } }