Download the PHP package scienta/doctrine-json-functions without Composer
On this page you can find all versions of the php package scienta/doctrine-json-functions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package doctrine-json-functions
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 per release
- Installation
- Testing
- Functions Registration
- Doctrine
- Symfony
- Usage
- Using Mysql 5.7+ JSON operators
- Using PostgreSQL 9.3+ JSON operators
- Using SQLite JSON operators
- DQL Functions
- Mysql 5.7+ JSON operators
- PostgreSQL 9.3+ JSON operators
- SQLite json1 extension operators
- Extendability and Database Support
- Architecture
- Adding new platform
- Adding new function
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
Functions Registration
Doctrine ORM
Doctrine documentation: "DQL User Defined Functions"
Symfony with Doctrine bundle
Symfony documentation: "DoctrineBundle Configuration"
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
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 @>
.
Boolean functions need to be registered as string functions and compared with true because Doctrine DQL does not know about boolean functions.
Using SQLite JSON operators
DQL Functions
The library provides this set of DQL functions.
Mysql 5.7+ JSON operators
- JSON_ARRAY_APPEND(json_doc, path, val[, path, val] ...)
- Appends values to the end of the indicated arrays within a JSON document and returns the result.
- JSON_ARRAYAGG(value)
- Aggregates a result set as a single JSON array whose elements consist of the rows.
- JSON_ARRAY_INSERT(json_doc, path, val[, path, val] ...)
- Updates a JSON document, inserting into an array within the document and returning the modified document.
- JSON_ARRAY([val[, val] ...])
- Evaluates a (possibly empty) list of values and returns a JSON array containing those values.
- JSON_CONTAINS_PATH(json_doc, one_or_all, path[, path] ...)
- Returns 0 or 1 to indicate whether a JSON document contains data at a given path or paths.
- JSON_CONTAINS(json_doc, val[, path])
- Returns 0 or 1 to indicate whether a specific value is contained in a target JSON document, or, if a path argument is given, at a specific path within the target document.
- JSON_DEPTH(json_doc)
- Returns the maximum depth of a JSON document.
- JSON_EXTRACT(json_doc, path[, path] ...)
- Returns data from a JSON document, selected from the parts of the document matched by the path arguments.
- JSON_OVERLAPS(json_doc1, json_doc2)
- Compares two JSON documents. Returns true (1) if the two document have any key-value pairs or array elements in common. If both arguments are scalars, the function performs a simple equality test.
- JSON_INSERT(json_doc, path, val[, path, val] ...)
- Inserts data into a JSON document and returns the result.
- JSON_KEYS(json_doc[, path])
- Returns the keys from the top-level value of a JSON object as a JSON array, or, if a path argument is given, the top-level keys from the selected path.
- JSON_LENGTH(json_doc[, path])
- Returns the length of JSON document, or, if a path argument is given, the length of the value within the document identified by the path.
- JSON_MERGE(json_doc, json_doc[, json_doc] ...)
- Merges two or more JSON documents and returns the merged result.
- JSON_MERGE_PRESERVE(json_doc, json_doc[, json_doc] ...)
- Merges two or more JSON documents and returns the merged result. Returns NULL if any argument is NULL. An error occurs if any argument is not a valid JSON document.
- JSON_MERGE_PATCH(json_doc, json_doc[, json_doc] ...)
- Performs an RFC 7396 compliant merge of two or more JSON documents and returns the merged result.
- JSON_OBJECT([key, val[, key, val] ...])
- Evaluates a (possibly empty) list of key/value pairs and returns a JSON object containing those pairs.
- JSON_OBJECTAGG(key, val)
- Takes two column names or expressions as arguments, the first of these being used as a key and the second as a value, and returns a JSON object containing key-value pairs.
- JSON_PRETTY(json_val)
- Provides pretty-printing of JSON values similar to that implemented in PHP and by other languages and database systems
- JSON_QUOTE(json_val)
- Quotes a string as a JSON value by wrapping it with double quote characters and escaping interior quote and other characters, then returning the result as a utf8mb4 string.
- JSON_REMOVE(json_doc, path[, path] ...)
- Removes data from a JSON document and returns the result.
- JSON_REPLACE(json_doc, path, val[, path, val] ...)
- Replaces existing values in a JSON document and returns the result.
- JSON_SEARCH(json_doc, one_or_all, search_str[, escape_char[, path] ...])
- Returns the path to the given string within a JSON document.
- JSON_SET(json_doc, path, val[, path, val] ...)
- Inserts or updates data in a JSON document and returns the result.
- JSON_TYPE(json_val)
- Returns a utf8mb4 string indicating the type of a JSON value.
- JSON_UNQUOTE(val)
- Unquotes JSON value and returns the result as a utf8mb4 string.
- JSON_VALID(val)
- Returns 0 or 1 to indicate whether a value is a valid JSON document.
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
- JSON_COMPACT(json_doc)
- Removes all unnecessary spaces so the json document is as short as possible.
- JSON_DETAILED(json_doc[, tab_size])
- Represents JSON in the most understandable way emphasizing nested structures.
- JSON_LOOSE(json_doc)
- Adds spaces to a JSON document to make it look more readable.
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
- expands to
- JSONB_EXISTS(jsonb, text)
- executed as
JSONB_EXISTS(jsonb, text)
, equivalent tojsonb ? text
- executed as
- JSONB_EXISTS_ALL(jsonb, array)
- executed as
JSONB_EXISTS_ALL(jsonb, array)
, equivalent tojsonb ?& array
- executed as
- JSONB_EXISTS_ANY(jsonb, array)
- executed as
JSONB_EXISTS_ANY(jsonb, array)
, equivalent tojsonb ?| array
- executed as
- JSONB_IS_CONTAINED(jsonb, jsonb)
- expands to
jsonb <@ jsonb
- expands to
- JSONB_INSERT
- executed as is
- JSON_EXTRACT_PATH
- executed as is
- JSON_GET(jsondoc, path)
- expands to
jsondoc->path
in case of numericpath
(use with JSON arrays) - expands to
jsondoc->'path'
in case of non-numericpath
(use with JSON objects)
- expands to
- JSON_GET_TEXT(jsondoc, path)
- expands to
jsondoc->>path
in case of numericpath
(use with JSON arrays) - expands to
jsondoc->>'path'
in case of non-numericpath
(use with JSON objects)
- expands to
- JSON_GET_PATH(jsondoc, path)
- expands to
jsondoc#>'path'
- expands to
- JSON_GET_PATH_TEXT(jsondoc, path)
- expands to
jsondoc#>>'path'
- expands to
Please note that chaining of JSON operators is not supported.
SQLite JSON1 Extension operators
Support for all the scalar and aggregate 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 ifjson
is some kind of JSON value other than an array.
- Returns the number of elements in the JSON 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
.
- Given zero or more sets of paths and values, it inserts (without overwriting) each value at its corresponding path of the
- 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
totarget
.
- Applies a
- JSON_QUOTE(value)
- Converts the SQL
value
(a number or a string) into its corresponding JSON representation.
- Converts the SQL
- JSON_REMOVE(json[, path], ...)
- Removes the values at each given
path
.
- Removes the values at each given
- 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
.
- Given zero or more sets of paths and values, it overwrites each value at its corresponding path of the
- 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
.
- Given zero or more sets of paths and values, it inserts or overwrites each value at its corresponding path of the
- JSON_TYPE(json[, path])
- Returns the type of the outermost element of
json
or of the value atpath
.
- Returns the type of the outermost element of
- JSON_VALID(json)
- Returns 1 if the argument
json
is well-formed JSON or 0 otherwise.
- Returns 1 if the argument
Aggregate functions
- JSON_GROUP_ARRAY(value)
- Returns a JSON array comprised of all
value
in the aggregation
- Returns a JSON array comprised of all
- JSON_GROUP_OBJECT(name, value)
- Returns a JSON object comprised of all
name/value
pairs in the aggregation.
- Returns a JSON object comprised of all
Extendability and Database Support
Architecture
Platform function classes naming rule is:
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.
All versions of doctrine-json-functions with dependencies
ext-pdo Version *
doctrine/dbal Version ^3.2 || ^4
doctrine/lexer Version ^2.0 || ^3.0
doctrine/orm Version ^2.19 || ^3