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
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With