PHPackages                             liqueurdetoile/cakephp-orm-json - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. liqueurdetoile/cakephp-orm-json

ActiveCakephp-plugin[Database &amp; ORM](/categories/database)

liqueurdetoile/cakephp-orm-json
===============================

Cakephp plugin to provide easy control over JSON type fields in database

2.0.0(3y ago)1463.9k↑11.8%3MITPHPPHP ^7.2|^8.0CI failing

Since Jul 19Pushed 3y ago1 watchersCompare

[ Source](https://github.com/liqueurdetoile/cakephp-orm-json)[ Packagist](https://packagist.org/packages/liqueurdetoile/cakephp-orm-json)[ RSS](/packages/liqueurdetoile-cakephp-orm-json/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (12)Versions (16)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/ea784850c89663b5cf54845f114951c48f4d7ccaac23e29bd68a3e5083fb36c4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6c6971756575726465746f696c652f63616b657068702d6f726d2d6a736f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/liqueurdetoile/cakephp-orm-json)[![2.x-next status](https://github.com/liqueurdetoile/cakephp-orm-json/actions/workflows/ci.yml/badge.svg?branch=2.x-next)](https://github.com/liqueurdetoile/cakephp-orm-json/actions/workflows/ci.yml/badge.svg?branch=2.x-next)[![Coverage Status](https://camo.githubusercontent.com/9b46a8fbfa2696158b8f5a7164a829ee515e58f602c4a871819bb56ee9ab8643/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6c6971756575726465746f696c652f63616b657068702d6f726d2d6a736f6e2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/liqueurdetoile/cakephp-orm-json?branch=master)[![PR_Welcome](https://camo.githubusercontent.com/8044932e4d65e1fbb9c1a6748c252052df35e41ac18b4a6548aba1ce19a72a40/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e)](https://camo.githubusercontent.com/8044932e4d65e1fbb9c1a6748c252052df35e41ac18b4a6548aba1ce19a72a40/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e)[![license](https://camo.githubusercontent.com/fed01eb8f83ca9af512558e335b5a1253d52e0102bed3fa7b27d13f8cbf5b81d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c6971756575726465746f696c652f63616b657068702d6f726d2d6a736f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/liqueurdetoile/cakephp-orm-json)

Cake-orm-json plugin
====================

[](#cake-orm-json-plugin)

**This branch is for CakePHP ^3.7|^4.0 and supports PHP ^7.2|^8.0****For previous CakePHP versions, please use v1 of this plugin**

This plugin extends usual CakePHP ORM operations with JSON fields. It embeds a special [datfield notation](#datfield-format) that allow to easily target a path into a JSON field data. With it, you can :

- select, order and filter queries : `$q = $table->find('json')->where(['jfield->darn.deep.key' => true])->all()`
- apply data types inside JSON data
- easily access, mutate and delete JSON data in entity : `$e->get('jfield->darn.deep.key')`
- use JSON data as foreign keys for associations (quite extreme indeed and not really recommended but it can be useful at margin)

**Relational databases are not primarily designed** to handle non-schemed data and using JSON data fields can issue really bad performances. Nevertheless the newest releases of engines have also shown significant improvements in dealing with JSON data and raising of NoSQL has created different needs and constraints.

**Caution : As with version 2.0.0, it only works with Mysql databases &gt;= 5.7.8. Setup is done to allow adding other engines to this plugin and I hope to release it at least for MariaDB and SQLite, maybe PostgreSQL. Any help would be very appreciated though :smile**

- [Cake-orm-json plugin](#cake-orm-json-plugin)
    - [Installation](#installation)
        - [Install plugin](#install-plugin)
        - [Recommended setup](#recommended-setup)
            - [Embeds `DatFieldAwareTrait` in models](#embeds-datfieldawaretrait-in-models)
            - [Embeds `DatFieldBehavior` in models](#embeds-datfieldbehavior-in-models)
            - [Enbeds `DatFieldTrait` with entities](#enbeds-datfieldtrait-with-entities)
    - [Datfield format](#datfield-format)
    - [Usage](#usage)
        - [Quick guide](#quick-guide)
        - [Selecting fields](#selecting-fields)
        - [Filtering and ordering data](#filtering-and-ordering-data)
        - [Using aggregation and functions](#using-aggregation-and-functions)
        - [Marshaling data](#marshaling-data)
        - [What brings `DatFieldTrait` within entities ?](#what-brings-datfieldtrait-within-entities-)
            - [For PHPStan users](#for-phpstan-users)
        - [Using JSON data types](#using-json-data-types)
        - [Using JSON data types](#using-json-data-types)
            - [Registering JSON data type permanently](#registering-json-data-type-permanently)
            - [Registering JSON data types for a single query](#registering-json-data-types-for-a-single-query)
        - [Linking models together](#linking-models-together)
            - [Limitations with CakePHP 3.x](#limitations-with-cakephp-3x)
    - [Advanced setup](#advanced-setup)- [Use the upgraded driver for all models](#use-the-upgraded-driver-for-all-models)- [Enable or disable upgraded driver per model](#enable-or-disable-upgraded-driver-per-model)- [Use upgraded driver per query](#use-upgraded-driver-per-query)- [Some tricky things to know](#some-tricky-things-to-know)
        - [API reference](#api-reference)
    - [Difference from v1.x](#difference-from-v1x)
    - [Changelog](#changelog)
    - [Disclaimer](#disclaimer)

Installation
------------

[](#installation)

### Install plugin

[](#install-plugin)

You can install the latest version of this plugin into your CakePHP application using [composer](http://getcomposer.org).

```
composer require liqueurdetoile/cakephp-orm-json
```

The base namespace of the plugin is `Lqdt\OrmJson`.

> **Important :** If you plan to use this plugin with Cakephp 3.x, you must enable compatibility mode by adding this line to your `config/bootstrap.php` in order to setup required classes alias from Cakephp 4.x :

```
\Lqdt\OrmJson\DatField\Compat3x::enable();
```

### Recommended setup

[](#recommended-setup)

This plugin is working by cloning the used connection in order to upgrade its driver and insert a translation step that will allow to parse datfield notation into a suitable form that can then be used by cakePHP ORM. Obviously, adding this layer if not using datfield notation is pretty useless though resource consuming.

There's many ways to setup the plugin in order to optimize things but we recommend this one as it will fit most of use cases :

- Add `DatFieldBehavior` that have JSON fields without upgrading table connection, and add `DatFieldTrait` to their corresponding entities;
- Add `DatFieldAwareTrait` to models without JSON fields but which uses associations relying on datfield foreign keys;
- Always call `find('datfields')` or `find('json')` when querying if using datfield notation to ensure that translation is correctly enabled.

Keep in my mind that you keep full control on using regular or upgraded connection. If you have some performance issues with this setup, please check [advanced setup](#advanced_setup) for more informations.

#### Embeds `DatFieldAwareTrait` in models

[](#embeds-datfieldawaretrait-in-models)

Usually, you will use this trait in models that needs to be linked to another model with a foreign key living in JSON data. The trait allows you to link models based on datfield foreign key(s) and to easily switch between regular or upgraded connection.

```

```

#### Embeds `DatFieldBehavior` in models

[](#embeds-datfieldbehavior-in-models)

Behavior brings up all of the convenience of [`DatFieldAwareTrait`](#embeds-datfieldawaretrait-in-models) and takes care of marshaling datfield notation when creating/patching entities. The behavior is targetted to models which contains JSON fields. It can also be used to store permanent JSON data types when marshaling or persisting data.

```
