monofone / DoctrineJsonFunctions

Doctrine DQL functions for SQL JSON data type

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DoctrineJsonFunctions

A set of extensions to Doctrine 2 that add support for json query functions.

Table of Contents

DQL Functions

This library provide set of DQL functions. Available Mysql functions:

Installation

Add the following dependency to your composer.json

{
	"require": {
		"syslogic/doctrine-json-functions": "dev-master"
	}
}

Functions Registration

Doctrine2

Doctrine2 Documentation: "DQL User Defined Functions"

<?php

use Syslogic\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);

Usage

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

<?php
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('c')
 ->from('Customer', 'c')
 ->where("JSON_CONTAINS(c.attributes, :certificates, '$.certificates') = 1");
 
 $result = $q->execute(array(
    'certificates' => '"BIO"'
    ));

Extendability and Database Support

Architecture

Platform function classes naming rule is:

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

Adding a new platform

To add support of new platform you just need to create new folder Syslogic\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.

About

Doctrine DQL functions for SQL JSON data type

License:MIT License


Languages

Language:PHP 100.0%