Doctrine MongoDB - is there any way to build a query from JSON?
Asked Answered
D

2

7

I have an arbitrary MongoDB JSON find query string, for example:

{ "address.city": "Seattle"}

or

{ qty: { $gt: 5, $lt: 50 }

Is there any existing method to create a Doctrine.MongoDB.Query object from the JSON string? Or alternately to query mongo directly and then pass those results to doctrine for hydration?

Diabolo answered 2/2, 2016 at 23:36 Comment(1)
It is a hack, but currently I am working around this by running an extra query. First JSON query runs against mongo directly and collects all the _ids as $idList. Then run the doctrine query as field('id')->in($idList)Diabolo
D
1

I haven't used it myself, but this recent bundle appears to have been created to serve this exact purpose (querybuilderjs into doctrine).

https://github.com/fourlabsldn/QBJSParserBundle

https://github.com/fourlabsldn/QBJSParser

         $parsedRuleGroup = $this->get('fl_qbjs_parser.json_query_parser.doctrine_orm_parser')->parseJsonString($jsonString, Product::class);

         $query = $this->get('doctrine.orm.entity_manager')->createQuery($parsedRuleGroup->getDqlString());
         $query->setParameters($parsedRuleGroup->getParameters());
         $results = $query->execute();
Diabolo answered 8/3, 2017 at 19:35 Comment(0)
M
1

Is there any existing method to create a Doctrine.MongoDB.Query object from the JSON string?

At this moment no, however we could add setQuery method to the Builder. More tedious, but working, would be instantiating Query class on your own.

Or alternately to query mongo directly and then pass those results to doctrine for hydration?

Once you have plain arrays with data (and you're not afraid to dive into UnitOfWork) you may utilize $dm->getUnitOfWork()->getOrCreateDocument() or employ HydratorFactory and merge documents into DocumentManager later.

Mongoloid answered 25/2, 2016 at 22:31 Comment(0)
D
1

I haven't used it myself, but this recent bundle appears to have been created to serve this exact purpose (querybuilderjs into doctrine).

https://github.com/fourlabsldn/QBJSParserBundle

https://github.com/fourlabsldn/QBJSParser

         $parsedRuleGroup = $this->get('fl_qbjs_parser.json_query_parser.doctrine_orm_parser')->parseJsonString($jsonString, Product::class);

         $query = $this->get('doctrine.orm.entity_manager')->createQuery($parsedRuleGroup->getDqlString());
         $query->setParameters($parsedRuleGroup->getParameters());
         $results = $query->execute();
Diabolo answered 8/3, 2017 at 19:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.