\Lqdt\OrmJson\ORMJsonQuery

Extends the core Query class to provide support for parsing valid queries containing JSON fields properties.

Summary

Methods
Properties
Constants
__construct()
isDatField()
jsonFieldName()
jsonFieldsNameinString()
rawSqlConverter()
comparisonConverter()
unaryExpressionConverter()
queryExpressionConverter()
expressionConverter()
jsonWhere()
jsonSelect()
parseSortConditions()
clause()
jsonOrder()
all()
No public properties found
No constants found
No protected methods found
$_isDotted
$_isJsonSorted
$_isAssoc
N/A
No private methods found
No private properties found
N/A

Properties

$_isDotted

$_isDotted : boolean

Stores the use of dots in fields names when selecting

Type

boolean

$_isJsonSorted

$_isJsonSorted : boolean

Stores the use of sorting on JSON fields values

Type

boolean

$_isAssoc

$_isAssoc : boolean

Stores the request to fetch back selected JSON fields as associative array

Type

boolean

Methods

__construct()

__construct(\Cake\Database\Connection  $connection, \Cake\ORM\Table  $table, \Cake\ORM\Query  $parentQuery = null) 

Constructor

Parameters

\Cake\Database\Connection $connection

Connection to use

\Cake\ORM\Table $table

Table to use *

\Cake\ORM\Query $parentQuery

Initial Query object

isDatField()

isDatField(string  $field) : boolean

Utility function to check if a field is datfield

Parameters

string $field

Field name

Returns

boolean

jsonFieldName()

jsonFieldName(String  $datfield) : String

Convert a property name given under datfield format into a valid JSON_EXTRACT short notation usable in cakePHP standard queries

Parameters

String $datfield

Input field name

Returns

String —

Returns Mysql valid formatted name to query JSON

jsonFieldsNameinString()

jsonFieldsNameinString(string  $expression) : string

Apply jsonFieldName to every property name detected in a string, mainly used to parse SQL fragments

The regexp is a bit tricky to avoid collision with mail parameter value that will be enclosed by quotes

Parameters

string $expression

SQL fragment to be reworked

Returns

string —

Parsed string that contains modified SQL fragment

rawSqlConverter()

rawSqlConverter(string  $expression) : \Cake\Database\Expression\QueryExpression

Returns a new QueryExpression built upon the parsing of the expression to update datfield names

Parameters

string $expression

Raw expression to transform

Returns

\Cake\Database\Expression\QueryExpression —

QueryExpression

comparisonConverter()

comparisonConverter(\Cake\Database\Expression\Comparison  $expression) : \Cake\Database\Expression\Comparison|\Cake\Database\Expression\QueryExpression

Update or replace the Comparison expression to perform comparisons on datFields. In some cases, PDO limitations implies to replace the expression with a raw SQL fragment. It can be a bit dangerous when using raw user input to perform global matching in `array` mode.

Regular fields expressions are left as is.

Parameters

\Cake\Database\Expression\Comparison $expression

Comparison expression

Returns

\Cake\Database\Expression\Comparison|\Cake\Database\Expression\QueryExpression —

Updated expression

unaryExpressionConverter()

unaryExpressionConverter(\Cake\Database\Expression\UnaryExpression  $expression) : \Cake\Database\Expression\UnaryExpression

Parses the unary expression to apply conversions on childrens and returns an updated UnaryExpression

Note : This a VERY hacky way because the UnaryExpression class doesn't expose getter/setter for protected _value property.

In this implementation, it causes an infinite loop when used directly with a SQL fragment :

['NOT' => 'sub.prop@datfield like "%buggy%"]

That's why, an exception is thrown as soon as $value is extracted

Parameters

\Cake\Database\Expression\UnaryExpression $expression

Expression

Returns

\Cake\Database\Expression\UnaryExpression —

New expression

queryExpressionConverter()

queryExpressionConverter(\Cake\Database\Expression\QueryExpression  $expression) : \Cake\Database\Expression\QueryExpression

Iterates over a QueryExpression and replace Comparison expressions to handle JSON comparison in datfields.

Parameters

\Cake\Database\Expression\QueryExpression $expression

QueryExpression

Returns

\Cake\Database\Expression\QueryExpression —

Updated QueryExpression

expressionConverter()

expressionConverter(string|\Cake\Database\ExpressionInterface  $expression) : \Cake\Database\ExpressionInterface

Returns the appropriate ExpressionInterface regarding the incoming one

Parameters

string|\Cake\Database\ExpressionInterface $expression

Incoming expression

Returns

\Cake\Database\ExpressionInterface —

Updated expression

jsonWhere()

jsonWhere(string|array|callable  $conditions) : \Lqdt\OrmJson\ORM\JsonQuery

Add conditions to the query that can be matched against datfield value

jsonWhere accepts exactly the same parameters than the regular Query::where method

The conditions are first parsed in a regular QueryExpression. The result is then converted to replace Comparison expressions in a suitable way to query JSON fields in database.

The conditions that are matching regular fields are kept intact, thus allowing to mix regular/datfields comparisons in a single call to jsonWhere.

Parameters

string|array|callable $conditions

Conditions for WHERE clause

Returns

\Lqdt\OrmJson\ORM\JsonQuery

Self for chaining

jsonSelect()

jsonSelect(string|array  $fields, string|boolean  $separator = '_', boolean  $lowercasedKey = false) : \Lqdt\OrmJson\ORM\JsonQuery

Adds support to fetch selected properties within a JSON field

Aliases fields are now supported as well as the use of a dot as separator or in alias for the resulting field name

Parameters

string|array $fields

Field name in datfield notation

string|boolean $separator

Separator string for field aliases name (dot is not allowed). Set it to false to return Resultset as associative array

boolean $lowercasedKey

Force key alias to be lowercased (useful when using models name in datfield that must be capitalized)

Returns

\Lqdt\OrmJson\ORM\JsonQuery

Self for chaining

parseSortConditions()

parseSortConditions(string|array  $conditions) : array

Parse sort options to fit `[["fieldname" => "direction"]]` pattern

Parameters

string|array $conditions

Sort conditions

Returns

array —

Parsed conditions

clause()

clause(string  $name) : array

Intercepts calls to clause('select') to enable or disable autofields

When sorting, we must select custom fields to sort and thus disable autofields even if no selection have been made. This wrapper prevents this behavior

Parameters

string $name

Clause name

Returns

array —

Array of clause

jsonOrder()

jsonOrder(string|array  $conditions) : \Lqdt\OrmJson\ORM\JsonQuery

Sets up the sorting based on JSON fields value

Parameters

string|array $conditions

Sorting conditions

Returns

\Lqdt\OrmJson\ORM\JsonQuery

self for chaining

all()

all() : \Cake\Datasource\ResultSetInterface

Wrapper for the genuine `\Cake\ORM\Query::all` method to allow dot in field names and sorting

When extracting data, it unescapes dotted field names and clean json fields used for sorting and not requested

Returns

\Cake\Datasource\ResultSetInterface —

Result set to iterate on