Sandboxing DQL queries in Doctrine2 - php

Is there a simple way to somehow filter DQL statements to be able to use DQL conditions in url query parameter for instance in a REST API? Like users/?q="firstName='John' AND contacts IS EMPTY".
Something like sandboxing mode in twig. Maybe a custom query walker could be used but probably someone has solved it already?

This is what custom Repository classes are for. In your custom Repository class, you can create oneor more methods that accept parameters and use the querybuilder to build up your query depending on the url parameters passed. See the documentation for the QueryBuilder class for many examples.

So i created a custom AST Walker. Looks like it works. But it's not fully tested yet.
https://gist.github.com/maryo/6762610

Related

In Symfony, how to get QueryBuilder from default findBy method?

In Symfony 5, using Doctrine, how can I get QueryBuilder object (instead of results) from default entity repository methods like findBy, findOneBy, findAll?
I need QueryBuilder for:
Passing it to KnpPaginator (requires specifically QueryBuilder instead of results)
Possibly extending it with additional query logic in the future
I could just write a simple query (like $em->createQuery("SELECT a FROM Article a")), but I want to have access to filtering and ordering provided by default findBy method. I think writing my own QueryBuilder with filtering/sorting by any property would be a lot of work and I'm not sure I could implement it well even if I tried.
EDIT: even though it does not answer my question exactly, I have found a solution for my 1st use case (using KnpPaginator without writing custom queries): Custom data repository pagination.
This method allows to attach pagination to any query without changing it instead of writing a new one through QueryBuilder.

zend framework muti-table query prototype

The tutorial of zend framework show that I can use Zend\Db\ResultSet\HydratingResultSet to return a model object using the dbAdapter ,select query and a model prototype. But in most case writing the website code I use multi-table query and the hydrator can not fix this problem but just a single table query. How should I deal with this kind of problem.
hydrator doc
And this problem can also happen when encounting the pagination which also only take one prototype.
paginator doc
Depending on the query, you might need a custom ResultSet that knows how to deal with the multi-row potential of a multi table query, and mapping that dataset appropriately.
Additionally, if you find yourself getting into very advanced object relationships with Zend\Db, that might be the time to start considering Doctrine2 (or a similar ORM).

Symfony2 Doctrine Querybuilder select all

I'm currently working on a Service in SF2 that queries the database with the QueryBuilder using a class variable set with a repository-specific QueryBuilder in the constructor of this Service.
Which means i would like to make use of this set QueryBuilder as much as possible for neater code and a clean feeling using it.
I want to avoid creating a query on the EntityManager, but instead solely query using this predefined Querybuilder.
I'm looking for something that would look/work like the following:
$query = $this->fooRepository->createQueryBuilder('f')->select('*');
return $query->getResult(Query::HYDRATE_ARRAY);
The above would (if it worked) return all the foo in the database as far as i know..
If you think i'm being stupid and should do something different in regard to the predefined QueryBuilders or just use the:
createQuery()
method because it simply isn't good practice or impossible, don't hesitate to tell me.
Thanks!
Try:
$qb = $this->fooRepository->createQueryBuilder('foo');
return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
No need for a select(*). All the foo items will be selected since no where clauses were added.

Doctrine2 - is there a pre-selection hook?

I'm developing a ws with Symfony2 / Doctrine2, and I'm trying to hook into Doctrine2 lifecycle and trigger a function before any SELECT action - but without any luck. Basically, i'd like to dynamically manipulate the selection queries adding limit/offset when certain parameters are found in the request, but it seems that Doctrine2 hooks concern only insert, delete and update actions. Am i missing something?
Thanks.
U have to use Doctrine Filter
Doctrine 2.2 features a filter system that allows the developer to add SQL to the conditional clauses of queries, regardless the place where the SQL is generated (e.g. from a DQL query, or by loading associated entities).
A way to deal with this would be to register a custom event that is dispatched before a select. You have already referenced the source that shows examples of how to do it.

php CRUD passing parameters

I'm working on a php service.
I'd like to pass an object with the parameters to use in the "where" clause and also the "order" and "limit" params.
I'm wondering whether there is a standard way/library to make up the query based on the parameters given, or whether I'll need to roll one.
I'm using ezsql to connect to mysql.
Any pointers, much appreciated.
Writing one yourself shouldn't be too hard. If you don't want to do so (DRY, right?), you may check out sites like phpclasses.org or use an abstraction layer from Doctrine (take a look at its QueryBuilder if it is what you need)
Another suggestion if you would like to use already existing code: Zend Framework has a "query builder", Zend_Db_Select.
This does not require the whole Zend Framework, but it does have some depedencies you will have to include.
Documentation: http://framework.zend.com/manual/en/zend.db.select.html

Categories