Symfony2 - how to search json_array field in findBy

doctrine-orm, json, php, symfony

Solution

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

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

Then you cam find here several solution to persist value object. http://rosstuck.com/persisting-value-objects-in-doctrine/

The first one (map it yourself) is the same as ZhukV and is applicable even if you keep an array.

Problem

Assume i have an array of data contacts, ``` $cont = array("123-123-123","sample@sample.com"); ``` Which i stored in symfony2 doctrine field as `json_array` type ``` $person->setContacts($cont); //Which automatically converts into json ``` Now my problem is, while searching the 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); ``` Does not yield the correct result, is there any other method to retrieve the data by `json_array` field,sorry if the question is too basic.

Original source