Giter Club home page Giter Club logo

doctrinejsonfunctions's Introduction

Latest Stable Version Total Downloads License

DoctrineJsonFunctions

A set of extensions to Doctrine 2+ that add support for json functions. +Functions are available for MySQL, MariaDb and PostgreSQL.

DB Functions
MySQL JSON_APPEND, JSON_ARRAY, JSON_ARRAYAGG, JSON_ARRAY_APPEND, JSON_ARRAY_INSERT, JSON_CONTAINS, JSON_CONTAINS_PATH, JSON_DEPTH, JSON_EXTRACT, JSON_OVERLAPS, JSON_INSERT, JSON_KEYS, JSON_LENGTH, JSON_MERGE, JSON_MERGE_PRESERVE, JSON_MERGE_PATCH, JSON_OBJECT, JSON_OBJECTAGG, JSON_PRETTY, JSON_QUOTE, JSON_REMOVE, JSON_REPLACE, JSON_SEARCH, JSON_SET, JSON_TYPE, JSON_UNQUOTE, JSON_VALID
PostgreSQL @> (JSONB_CONTAINS), ? (JSONB_EXISTS), ?& (JSONB_EXISTS_ALL), ?| (JSONB_EXISTS_ANY), <@ (JSONB_IS_CONTAINED), JSONB_INSERT, JSON_EXTRACT_PATH, -> (JSON_GET), #> (JSON_GET_PATH), #>> (JSON_GET_PATH_TEXT), ->> (JSON_GET_TEXT)
MariaDb JSON_VALUE, JSON_EXISTS, JSON_QUERY, JSON_COMPACT, JSON_DETAILED, JSON_LOOSE, JSON_EQUALS, JSON_NORMALIZE
SQLite JSON, JSON_ARRAY, JSON_ARRAY_LENGTH, JSON_EXTRACT, JSON_GROUP_ARRAY, JSON_GROUP_OBJECT, JSON_INSERT, JSON_OBJECT, JSON_PATCH, JSON_QUOTE, JSON_REMOVE, JSON_REPLACE, JSON_SET, JSON_TYPE, JSON_VALID

Table of Contents

Changelog

Changes per release are documented with each github release. You can find an overview here: https://github.com/ScientaNL/DoctrineJsonFunctions/releases

Installation

The recommended way to install DoctrineJsonFunctions is through Composer.

Run the following command to install the package:

composer require scienta/doctrine-json-functions

Alternatively, you can download the source code as a file and extract it.

Testing

This repository uses phpunit for testing purposes. If you just want to run the tests you can use the docker composer image to install and run phpunit. There is a docker-compose file with the correct mount but if you want to use just docker you can run this:

php8

docker run -it -v ${PWD}:/app scienta/php-composer:php8 /bin/bash -c "composer install && ./vendor/bin/phpunit"

Functions Registration

Doctrine ORM

Doctrine documentation: "DQL User Defined Functions"

<?php

use Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql as DqlFunctions;

$config = new \Doctrine\ORM\Configuration();
$config->addCustomStringFunction(DqlFunctions\JsonExtract::FUNCTION_NAME, DqlFunctions\JsonExtract::class);
$config->addCustomStringFunction(DqlFunctions\JsonSearch::FUNCTION_NAME, DqlFunctions\JsonSearch::class);

$em = EntityManager::create($dbParams, $config);
$queryBuilder = $em->createQueryBuilder();

Symfony with Doctrine bundle

Symfony documentation: "DoctrineBundle Configuration"

# config/packages/doctrine.yaml
doctrine:
    orm:
        dql:
            string_functions:
                JSON_EXTRACT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonExtract
                JSON_SEARCH: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonSearch

Note that doctrine is missing a boolean_functions entry. You can register boolean functions as string_functions and need to compare them with = true to avoid DQL parser errors. For example, to check for existence of an element in a JSONB array, use andWhere('JSONB_EXISTS(u.roles, :role) = true).

Usage

Mind the comparison when creating the expression and escape the parameters to be valid JSON.

Using Mysql 5.7+ JSON operators

$q = $queryBuilder
  ->select('c')
  ->from('Customer', 'c')
  ->where("JSON_CONTAINS(c.attributes, :certificates, '$.certificates') = 1");

$result = $q->execute(array(
  'certificates' => '"BIO"',
));

Using PostgreSQL 9.3+ JSON operators

Note that you need to use the function names. This library does not add support for custom operators like @>.

$q = $queryBuilder
  ->select('c')
  ->from('Customer', 'c')
  ->where("JSON_GET_TEXT(c.attributes, 'gender') = :gender");

 $result = $q->execute(array(
    'gender' => 'male',
 ));

Boolean functions need to be registered as string functions and compared with true because Doctrine DQL does not know about boolean functions.

$q = $queryBuilder
  ->select('c')
  ->from('Customer', 'c')
  ->where('JSONB_CONTAINS(c.roles, :role) = true');

 $result = $q->execute(array(
    'role' => 'ROLE_ADMIN',
 ));

Using SQLite JSON operators

$q = $queryBuilder
  ->select('c')
  ->from('Customer', 'c')
  ->where("JSON_EXTRACT(c.attributes, '$.gender') = :gender");

 $result = $q->execute();

DQL Functions

The library provides this set of DQL functions.

Mysql 5.7+ JSON operators

Note that you can use MySQL Operators with MariaDb database if compatible.

MariaDb 10.2.3 JSON operators

  • JSON_VALUE(json_doc, path)
    • Returns the scalar specified by the path. Returns NULL if there is no match.
  • JSON_EXISTS(json_doc, path)
    • Determines whether a specified JSON value exists in the given data. Returns 1 if found, 0 if not, or NULL if any of the inputs were NULL.
  • JSON_QUERY(json_doc, path)
    • Given a JSON document, returns an object or array specified by the path. Returns NULL if not given a valid JSON document, or if there is no match.

MariaDb 10.2.4 JSON operators

MariaDb 10.7.0 JSON operators

  • JSON_EQUALS(json_doc, json_doc)
    • Checks if there is equality between two json objects. Returns 1 if it there is, 0 if not, or NULL if any of the arguments are null.
  • JSON_NORMALIZE(json_doc)
    • Recursively sorts keys and removes spaces, allowing comparison of json documents for equality.

PostgreSQL 9.3+ JSON operators

Basic support for JSON operators is implemented. This works even with Doctrine\DBAL v2.5. Official documentation of JSON operators.

  • JSONB_CONTAINS(jsonb, jsonb)
    • expands to jsonb @> jsonb
  • JSONB_EXISTS(jsonb, text)
    • executed as JSONB_EXISTS(jsonb, text), equivalent to jsonb ? text
  • JSONB_EXISTS_ALL(jsonb, array)
    • executed as JSONB_EXISTS_ALL(jsonb, array), equivalent to jsonb ?& array
  • JSONB_EXISTS_ANY(jsonb, array)
    • executed as JSONB_EXISTS_ANY(jsonb, array), equivalent to jsonb ?| array
  • JSONB_IS_CONTAINED(jsonb, jsonb)
    • expands to jsonb <@ jsonb
  • JSONB_INSERT
    • executed as is
  • JSON_EXTRACT_PATH
    • executed as is
  • JSON_GET(jsondoc, path)
    • expands to jsondoc->path in case of numeric path (use with JSON arrays)
    • expands to jsondoc->'path' in case of non-numeric path (use with JSON objects)
  • JSON_GET_TEXT(jsondoc, path)
    • expands to jsondoc->>path in case of numeric path (use with JSON arrays)
    • expands to jsondoc->>'path' in case of non-numeric path (use with JSON objects)
  • JSON_GET_PATH(jsondoc, path)
    • expands to jsondoc#>'path'
  • JSON_GET_PATH_TEXT(jsondoc, path)
    • expands to jsondoc#>>'path'

Please note that chaining of JSON operators is not supported.

SQLite JSON1 Extension operators

Support for all the scalar and aggregare functions as seen in the JSON1 Extension documentation.

Scalar functions

  • JSON(json)
    • Verifies that its argument is a valid JSON string and returns a minified version of that JSON string.
  • JSON_ARRAY([val[, val] ...])
    • Accepts zero or more arguments and returns a well-formed JSON array that is composed from those arguments.
  • JSON_ARRAY_LENGTH(json[, path])
    • Returns the number of elements in the JSON array json, or 0 if json is some kind of JSON value other than an array.
  • JSON_EXTRACT(json, path[, path ], ...)
    • Extracts and returns one or more values from the well-formed JSON.
  • JSON_INSERT(json[, path, value],...)
    • Given zero or more sets of paths and values, it inserts (without overwriting) each value at its corresponding path of the json.
  • JSON_OBJECT(label, value[, label, value], ...)
    • Accepts zero or more pairs of arguments and returns a well-formed JSON object that is composed from those arguments.
  • JSON_PATCH(target, patch)
    • Applies a patch to target.
  • JSON_QUOTE(value)
    • Converts the SQL value (a number or a string) into its corresponding JSON representation.
  • JSON_REMOVE(json[, path], ...)
    • Removes the values at each given path.
  • JSON_REPLACE(json[, path, value],...)
    • Given zero or more sets of paths and values, it overwrites each value at its corresponding path of the json.
  • JSON_SET(json[, path, value],...)
    • Given zero or more sets of paths and values, it inserts or overwrites each value at its corresponding path of the json.
  • JSON_TYPE(json[, path])
    • Returns the type of the outermost element of json or of the value at path.
  • JSON_VALID(json)
    • Returns 1 if the argument json is well-formed JSON or 0 otherwise.

Aggregate functions

  • JSON_GROUP_ARRAY(value)
    • Returns a JSON array comprised of all value in the aggregation
  • JSON_GROUP_OBJECT(name, value)
    • Returns a JSON object comprised of all name/value pairs in the aggregation.

Extendability and Database Support

Architecture

Platform function classes naming rule is:

Scienta\DoctrineJsonFunctions\Query\AST\Functions\$platformName\$functionName

Adding a new platform

To add support of new platform you just need to create new folder Scienta\DoctrineJsonFunctions\Query\AST\Functions\$platformName and implement required function there according to naming rules

Adding a new function

If you want to add new function to this library feel free to fork it and create pull request with your implementation. Please, remember to update documentation with your new functions.

See also

dunglas/doctrine-json-odm: Serialize / deserialize plain old PHP objects into JSON columns.

doctrinejsonfunctions's People

Contributors

aalwash avatar annervisser avatar ayrtonricardo avatar bpolaszek avatar dbu avatar derrabus avatar ghdi avatar gronostajo avatar guillemfondin avatar harmenm avatar hayrie avatar hermzz avatar hikariii avatar jontsa avatar juliencabanes avatar mbrodala avatar monofone avatar mvn-h24 avatar norkunas avatar nyrodev avatar piotaixr avatar scottfalkingham avatar simpod avatar thiagoflessak avatar tobalsan avatar tobion avatar ttk avatar willemverspyck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

doctrinejsonfunctions's Issues

Error on use JSON_EXTRACT with named properties

How to reproduce

in the database field I store this JSON document

{
  "site": "2dbc9c35-bf39-433c-b596-6b08aa941bac",
  "bitrix24": 123456,
  "mobile_backend": "5624"
}

In read model I try to fetch documents with bitrix24 = 123456

$qb
->andwhere("JSON_EXTRACT(EXTERNAL_IDS, '$.bitrix24') = :external_system_id")
->setParameter(':external_system_id', 123456);
        
print_r($qb->getSQL());
print_r($qb->getParameters());

dump:

SELECT
 contacts.UUID as contact_id, 
 contacts.NAME as contact_name,
 contacts.EXTERNAL_IDS as contact_external_ids 
FROM 
test_table
WHERE
 (contacts.APP_CLIENT_ID =: APP_CLIENT_ID)
 AND
 (JSON_EXTRACT(EXTERNAL_IDS, '$.bitrix24') =: external_system_id)
Array
(
[: APP_CLIENT_ID] => 106
[: external_system_id] => 123456
)

But the select result is empty, if i hardcode parameters in SQL - it works, is I use sprintf to build sql-query they work too.

If i use parameters for both arguments

$qb->andwhere("JSON_EXTRACT(EXTERNAL_IDS, '$.:external_system_code') = :external_system_id")
 ->setParameter(':external_system_code', 'bitrix24')
 ->setParameter(':external_system_id', 123456);

i got an error in SQL query

An exception occurred while executing 
'
SELECT
 count(*) as cnt 
FROM 
(SELECT 
contacts.UUID as contact_id, 
contacts.NAME as contact_name,
contacts.EXTERNAL_IDS as contact_external_ids 
FROM test_table
WHERE
 (contacts.APP_CLIENT_ID = ?) AND (JSON_EXTRACT(EXTERNAL_IDS, '$.:external_system_code') = ?)) dbal_count_tbl' 

with params [106, 123456]:\n\nSQLSTATE[42000]: Syntax error or access violation: 3143 Invalid JSON path expression. The error is around character position 23.

Looking for an example of JSON_SET()

Hi,
Can someone show me an example how to use MySQL JSON_SET() with this extension? I successfully use it for Select operations.
How to set this $path into a set($path, $value) ?
"JSON_SET(u.json_info, '$.".$path."', JSON_QUOTE(:value)) = 1")

Thanks in advance.

How to integrate this extension with symfony2

Hi,

Thank you very much for developing this module. Let me know the steps that I should follow to integrate this extension to symfony2 with config.yml file (symfony config.yml).

Thanks

Add Index on a query using JSON_GET or other methods

More a question. I want to add a index on a specific JSON query I know its possible via PostgreSQL the query looks like this:

$queryBuilder->leftJoin(
    Contact::class,
    'contact',
    Join::WITH,
    'CAST(JSON_GET(JSON_GET(visit.data, \'code\'), \'contactId\') AS INT) = contact.id',
)

It is a third party entity and I wanted to use Events::loadClassMetadata to add a index but the following:

$builder = new ClassMetadataBuilder($metadata);
$builder->addIndex(['CAST(JSON_GET(JSON_GET(visit.data, \'code\'), \'contactId\') AS INT)'], 'data_code_contactId_idx');

because it is not a column maybe somebody achieved something like this, without that the ORM is throwing the index away on the next migration.

Operation 'JSON_CONTAINS' is not supported by platform. (MYSQL 5.7)

$platform = $sqlWalker->getConnection()->getDatabasePlatform();

I am running mysql 5.7
get_class($platform)returns "Doctrine\DBAL\Platforms\MySqlPlatform"

But I got this error:

Operation 'JSON_CONTAINS' is not supported by platform.
500 Internal Server Error - DBALException

If I remove if ($platform instanceof MySQL57Platform) { from JsonContains it works nice

JSON_EXTRACT missing in MariaDB

I can use JSON_EXTRACT in MariaDB, but the package here does not seem to support it since I updated to symfony 7 & doctrine/dbal 4

I'm not sure what additional information I could provide to help debug that. But also on the README of the project, JSON_EXTRACT is not listed in the MariaDB column.

Incompatibility with doctrine/dbal 3

When upgraded to doctrine/dbal 3, an exception Attempted to load class "DBALException" from namespace "Doctrine\DBAL".
Because this class does not exist anymore.

This classe is used by PostgresqlJsonOperatorFunctionNode, and check if the Platform is an instance of PostgreSqlPlatform, which is wrong in doctrine/dbal 3, the instance is now a PostgreSQL100Platform

This happens, because the compose.json file of scienta/doctrine-json-functions does not have any constraint on 3rd party library.
I think you should add constraints in the require section. Or, if you want to keep this section optional, you should add the constraint in the conflict section to prevent people from using versions that are not tested.

How to use JSON_OBJECT with setParameters

Hi,

How can I use JSON_OBJECT with setParameters?

When try to use it like this:

$qb = $em->createQueryBuilder()
    ->select('count(q)')
    ->from(MyClass::class, q)
    ->andWhere('q.myArray = JSON_OBJECT(:key, :value)')
    ->setParameters([
        'key' => "name",
        'value' => "Max"
    ])
    
$ab->getQuery()->getSingleScalarResult();

I get following error:

[Doctrine\ORM\Query\QueryException] [Syntax Error] line 0, col 122: Error: Expected string, got ':utmParameterKey'

Thank you for your help!

Michi

The MariaDB JSON functions are no longer processed

The MariaDB JSON functions are no longer processed as of doctrine/dbal:4.0 because the platform abstraction has changed there.

\Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mariadb\MariadbJsonFunctionNode::validatePlatform

The class mentioned checks for the MySQLPlatform. However, MariaDb now only belongs to it in an abstract way. It would have to check for AbstractMySQLPlatform.

JSON_GET_TEXT doesn't accept a parameter as second argument

Hello,

Much like #39 the function JSON_GET_TEXT does not seem to support an argument in a query builder:

'JSON_GET_TEXT(event.visibility, :role) = \'true\'''

Brings the following error:

Error: Expected numeric, got ':role'"

The same change (switching from self::ALPHA_NUMERIC to self::VALUE_ARG in the requiredArgumentTypes) seems to solve the problem.

This seems to have been brought by commit 5f92c6a last december.

The bug (and fix) should probably be extended to all classes that were added requirements. I wonder what security bug this could introduce ?

Not working with doctrine/orm 3.0 and doctrine/lexer 3.0

Hi !

Symfony: 7.0.3
Doctrine ORM: 3.0
Doctrine Lexer: 3.0

When trying to use JSON_GET_TEXT on postgres DB like this

public function findAllByRoles(string $role): array 
{
        $qb = $this->createQueryBuilder('u');
        $qb
            ->where("JSON_GET_TEXT(u.roles, 0) = :role")
            ->setParameter('role', $role)
            ->orderBy('u.lastname', 'ASC')
            ->addOrderBy('u.firstname', 'ASC');

        return $qb->getQuery()->getResult();
}

I get this error
Undefined constant Doctrine\ORM\Query\Lexer::T_IDENTIFIER

Before doctrine/orm and doctrine/lexer 3.0, tokens are stored directly as constants. But now (in 3.0) tokens are stored in TokenType Enum

Allow double quotes in all functions

Hi ๐Ÿ‘‹

As it turns out, all functions currently support this:

JSON_EXTRACT(my_field, '$.prop')

but do not accept this:

JSON_EXTRACT(my_field, "$.prop")

This fails with a parse error:

Doctrine\ORM\Query\QueryException: [Syntax Error] line 0, col 64: Error: Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression, got '"'

In SQL both is perfectly valid and it feels weird, that this makes a difference here.
Is this an issue in doctrine or in this library?

Thanks for your great work!

Support doctrine generated column

Hi @Hikariii I use this channel to communicate with you since I don't want to reopen a old doctrine issue.

You open a issue/pull request for doctrine to support readonly (allowing generated column). It have been rejected since they didn't want to introduce something that they will deprecated right away for doctrine 3.0. And here we are more than 2 years later and doctrine 3.0 still not there, so the "right away" would still have been usefull for a long period.

So I am just wondering how you solved the issue ?

Allow integers for PostgreSQL JSON operators

Maybe there is a proper way to do this, but I did not find it. I want to select the first value of a JSON array, that is stored property in database like: ["VALUE"].

According to https://www.postgresql.org/docs/9.3/functions-json.html, the operator I should use is:

["VALUE"]->>0

For some reason I can't target the integer value when using JSON_GET_TEXT(jsondoc, path). If I go

$query->andWhere("JSON_GET_TEXT(i.value, 0) = :fieldValue)
      ->setParameter('fieldValue', 'VALUE');

I get the following error:

Error: Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression, got '0'

If I send the zero as a string, like this:

$query->andWhere("JSON_GET_TEXT(i.value, '0') = :fieldValue)
      ->setParameter('fieldValue', 'VALUE');

The genrated SQL will look like this: ["VALUE"]->>'0' and it won't give the desired results.

I found a workaround, but there has to be a better way to do this. I wrap up the index number into ABS function like this:

$query->andWhere("JSON_GET_TEXT(i.value, ABS(0)) = :fieldValue)
      ->setParameter('fieldValue', 'VALUE');

JSON_QUOTE doesn't accept a placeholder

Hello there,

When doing a prepared DQL statement containing this:

JSON_QUOTE(:foo)

Then binding :foo to a scalar value, here's the error I get:

[Syntax Error] line 0, col 93: Error: Expected string, got ':foo'

I can solve this by changing this code:

# src/Query/AST/Functions/Mysql/JsonQuote.php

class JsonQuote extends MysqlJsonFunctionNode
{
    const FUNCTION_NAME = 'JSON_QUOTE';

    /** @var string[] */
    protected $requiredArgumentTypes = [self::STRING_ARG];
}

To this one:

# src/Query/AST/Functions/Mysql/JsonQuote.php

class JsonQuote extends MysqlJsonFunctionNode
{
    const FUNCTION_NAME = 'JSON_QUOTE';

    /** @var string[] */
    protected $requiredArgumentTypes = [self::VALUE_ARG];
}

But would there be any drawbacks of doing this?

Tested platform: MySQL 5.7.

Thank you,
Ben

PostgreSQL: Can you use JSON_GET on JSONB columns deeper than one layer?

Like the title says, can you use JSON_GET on JSONB columns deeper than one layer for PostgreSQL databases?

I have a table called sales_order that contains a JSONB column called sales_order_data. This column contains json roughly in the shape of:

{
  "entity": {
    "billing_address": {
      "last_name": "foobar"
    }
  },
  "identifier": "1234"
}

Via regular queries directly in my PostgreSQL shell like
SELECT * FROM sales_order WHERE sales_order_data->'entity'->'billing_address'->'last_name' ? 'foobar';
I am able to retrive data (I know that this would be closer to JSONB_EXISTS).

But I seem to be unable to replicate the same behavior via Doctrine QueryBuilder and this extension.
Funnily enough I am able to access the "identifier" field with

$q = $this->createQueryBuilder('so')
            ->select('so')
            ->where("JSON_GET_TEXT(so.salesOrderData, 'identifier') = :identifier")
            ->setParameter('identifier', '1234')
            ->getQuery()->getArrayResult();

but I couldn't for example get billing_address and last_name below it which is why I have the feeling, that this library can only go down one layer into JSON data.

Is that the case?

JSON_EXTRACT: Property names with spaces

Hi,

The way function is designed, it does not account for property names with spaces it seems. For example, I have a search criteria JSON_EXTRACT(field, '$.results."heat exchange"')

Above condition is valid as per MySQL docs wherein if a property name has space character in between, it must be enclosed in quotation marks. This however generates an error:

[Syntax Error] line 0, col 113: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '"'

MySQL info on this matter: http://bit.ly/2EVbkX2

Thoughts on support for SQLite?

Hi,

For our project, we could really use this library, but (basic) SQLite support is a hard requirement for our userbase.

I've done some preliminary testing, and it seems that at least JSON_EXTRACT will work on sqlite as well. Is SQLite support for this package overall something to be considered?

wildcard in JSON_CONTAINS

Hey there
First of all, thanks for your lovely extension, it may make me win some time. ๐Ÿ‘

I just have one problem, and I don't manage fo find any solution, maybe you could help.
I'm trying to find every row who contains (in a JSON field) a specific value, for a specific path. The problem is that the path contains an array.

Here's my request :
SELECT l FROM AppBundle:Letter l WHERE JSON_CONTAINS(l.metadatas, :value, '$.papers[*].quality') = 1";

'papers' is an array with possibly a lot of different element. I'd like to know if I can wildcard the index to find rows with at least one paper element whose quality is equal to :value

Thanks a lot ;)

What is Postgresql configuration?

Hello ,

this is symfony configuration for MySQL:

# config/packages/doctrine.yaml
doctrine:
    orm:
        dql:
            string_functions:
                JSON_EXTRACT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonExtract
                JSON_SEARCH: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonSearch

What is the configuration for Postgresql?
Thank's

JSON_MERGE error

Seem that Json merge is not working

Notice: Undefined property: Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonMerge::$firstJsonPathExpr

Since this is in line 64 of JsonMerge
The variable is wrong, maybe this is $this->firstJsonDocExpr not $this->firstJsonPathExpr

JSONB_CONTAINS : invalid token

Sorry but I can't find a proper example of the function for postgres
running in symfony 7 and latest doctrine 3

I have a column location containing a JSONB like this:

{title:"blah blah", coordinates:..., etc ...}

I execute this query

        $qb = $this->createQueryBuilder('a');
        $qb = $qb->select('a.id', 'a.location');
        $qb = $qb->where("JSONB_CONTAINS(a.location, 'title') LIKE :loc0");
        $qb = $qb->setParameter('loc0', ' % blah% ');
        $query = $qb->getQuery();
        $result = $query->getResult();

error

                                                                                              
  SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for type json  
  LINE 1: ...AS location_1 FROM post p0_ WHERE p0_.location @> 'title' LI...                  
                                                               ^                              
  DETAIL:  Token "title" is invalid.                                                          
  CONTEXT:  JSON data, line 1: title      

conf:

        dql:
            numeric_functions:
                rand: DoctrineExtensions\Query\Mysql\Rand
            string_functions:
                    JSONB_CONTAINS: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Postgresql\JsonbContains

Added to packagist

Hi,

I added it to packagist, so it isn't necessary to add the repository anymore.

Greetings

Same namespace for 2 different classes

Hey @Hikariii,
First I want to thank you for this library.

When I'm installing from composer I have the following Warning:

Warning: Ambiguous class resolution, "Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Postgresql\JsonSharpGt" was found in both "/.../vendor/syslogic/doctrine-json-functions/src/Query/AST/Functions/Postgresql/JsonSharpGtGt.php" and "/.../vendor/syslogic/doctrine-json-functions/src/Query/AST/Functions/Postgresql/JsonSharpGt.php", the first will be used.

When I take a look at it, you have two classes with the same name:

  1. JsonSharpGt.php named JsonSharpGt
  2. JsonSharpGtGt.php named JsonSharpGt

Since they get the same FUNCTION_NAME I'm not sure about what to do (I never used Postgresql). If you now what has to be done I can provide a PR ๐Ÿ˜‰

How to find integer values

If my (simple) JSONB array contains integers/numbers (non-strings), there seems to be no way to find them. JSONB_EXISTS, etc, searches only for string values.

JSON_EXTRACT for LEFT JOIN

Hi,

I am using a property on the JSON document as a foreign key, utilising the JSON_EXTRACT function as part of a LEFT JOIN,

When doctrine (2.4.8) comes to hydrate the objects, it requires a "parentAliasMap" in order to merge the joined result on to the entity. (Doctrine\ORM\Internal\Hydration\ObjectHydrator.php:393)

Is it possible to define a relationship on an entity class this way? (Or at least trick Doctrine in to dynamically creating the relationship on the entity result.

Please let me know if this question needs any clarification. I look forward to your response.

Thank you,

Daniel

JSONB_CONTAINS and ::jsonb

JSONB_CONTAINS is included in the codebase under the Postgres section, but isn't mentioned in the documentation. It appears to generate the correct SQL with the exception of not casting either parameter with ::jsonb.

Is there a way to use JSONB_CONTAINS with ->setParameter() that will result in the field reference and the parameter value being followed by ::jsonb?

I was able to use createNativeQuery and ResultSetMappingBuilder and get the expected result, and I've been able to write an OrX condition using JSON_GET_PATH_TEXT to or together several paths, but I'd prefer to use querybuilder and JSONB_CONTAINS if possible.

MySQL: Explicit evaluation of JSON_CONTAINS() = 1 forces looping through all rows, bypassing index.

Hello, i found a bug in a pretty niche use case.

MySQL provides Multi-Valued Indexes to speed up JSON_CONTAINS() search. However, when the condition JSON_CONTAINS(โ€œ...โ€) = 1 is evaluated, MySQL must execute the function for each row to determine the outcome before it can apply the =1 condition.
This basically bypass the index, and makes query slow to a crawl on big tables.

Removing the "=1" will causes Doctrine to fails to parse the query, throwing a very non descriptive error "Warning: Attempt to read property "type" on null". Took me quite awhile to trace the problem to this library.

Going forward, can you disable the check for "=1"? This is redundant, as simply call JSON_CONTAINS(โ€œ...โ€) will use the index as expected, while also only return rows when this is implicitly true.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.