Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine ORM - how to map jsonb / yaml?

In my project I'm using Doctrine doctrine 2.5.4/postgres 9.5. I'm trying to add a jsonb field using yaml:

fields:
  obj: json_array

This gets converted to json (and not jsonb). The specification notes about picking up json or jsonb:

Chosen if the column definition contains the jsonb option inside the platformOptions attribute array and is set to true.

But platformOptions doesn't seems to work (tried to add it below obj, at the top... with no success). How can I add a jsonb field?

thanks, dan

like image 909
Dani Magen Avatar asked May 19 '16 08:05

Dani Magen


People also ask

How do I query Jsonb data?

Querying the JSON document PostgreSQL has two native operators -> and ->> to query JSON documents. The first operator -> returns a JSON object, while the operator ->> returns text. These operators work on both JSON as well as JSONB columns. There are additional operators available for JSONB columns.

How is Jsonb stored?

JSONB objects are stored as a decompressed binary as opposed to "raw data" in JSON, where no reparsing of data is required during retrieval. JSONB also supports indexing, which can be a significant advantage.

Is Jsonb faster than JSON?

Json processes input faster than jsonb as there is no conversion involved in this. Jsonb converts the JSON data into the binary form so it has slightly slower input due to the binary conversion overhead. There is no change in the Schema design while working with JSON.

Should I use Jsonb or JSON?

In general, most applications should prefer to store JSON data as jsonb , unless there are quite specialized needs, such as legacy assumptions about ordering of object keys. RFC 7159 specifies that JSON strings should be encoded in UTF8.


2 Answers

This is supported by doctrine/dbal v2.6+ (it requires PHP 7.1). All you need to do is use json_array and set options={"jsonb"=true} I tested this on doctrine/dbal v2.6.3

This is what it looks like in PHP format

/**
 * @ORM\Column(type="json_array",nullable=true,options={"jsonb"=true})
 */
private $details;

and it creates query such as (for existing talble):

ALTER TABLE mytable ADD details JSONB NOT NULL;

more details about type mapping can be found at Doctrine mapping matrix.

like image 103
Evren Yurtesen Avatar answered Sep 20 '22 14:09

Evren Yurtesen


Use boldtrn/jsonb-bundle, it provides a jsonb doctrine type as well as custom functions to access the special operators provided by the PostgreSQL jsonb data type.

like image 35
aferber Avatar answered Sep 18 '22 14:09

aferber