Symfony2 - how to look for json_array field in findBy

Suppose I have an array of data contacts,

$cont = array("123-123-123"," sample@sample.com "); 

What I saved in the symfony2 doctrine field as json_array type

 $person->setContacts($cont); //Which automatically converts into json 

Now my problem is that when searching for a person by contact

 $cont['contacts'] = array("123-123-123"," sample@sample.com "); or $cont['contacts'] = json_encode(array("123-123-123"," sample@sample.com ")); $person->findBy($cont); 

It does not give the correct result, is there any other method for retrieving data in the json_array field, sorry if the question is too simple.

+7
json php symfony doctrine2
source share
3 answers

You should not use JSON to store in the database if you want to search on it.

What you are trying to do is basically save the value object (you should create a real Contact value object instead of using an array).

Then you buy here several solutions to preserve the value object. http://rosstuck.com/persisting-value-objects-in-doctrine/

The first (draw it yourself) is the same as ZhukV, and is applicable even if you are storing an array.

+4
source share

You can use the json (or jsonb) data type if you are using PostgreSQL> 9.3.

You can read the documentation here ( Datatype JSON Documentation ).

+2
source share

JSON searches are a bad idea for SQL databases. You can create other fields (phone and email address).

All values ​​before binding to PDO are automatically converted to DBAL \ Types, and you must set the data as a parameter (object, array, etc.).

 $cont['contacts'] = array("123-123-123"," sample@sample.com "); $person->findBy($cont); 

This is working code. You can view the profiler to view the full SQL query.

-2
source share

All Articles