mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Disabling query cache from builder #5153
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @danielcuervo on GitHub (Jun 15, 2016).
Hello,
I am writing this because I have been working in some issues we had with the query cache together with using the paginators.
I wanted to disable the query cache for some specific queries, but it was imposible to disable them from the query builder that we were passing to the paginator class. The thing was that when the builder was creating the query through the "getQuery()" function within the paginator the _useQueryCache was giving true, since "setCacheable" on builder only changes the "cacheable" property. This behaviour was a bit confusing and hard to find what was the issue.
My question is if it would make sense to have access to the useQueryCache through the builder aswell, that would mean in order to be able to disable the query cache for every instance that is created with that builder:
`
`
If you think it makes sense I would gladly do the code and open a pull request, otherwise, just close the issue.
Cheers.
@Ocramius commented on GitHub (Jun 15, 2016):
If we start replicating all the API of the query object in the query builder, it will never be over. Is there any reason why you can't disable caching on the query object?
@danielcuervo commented on GitHub (Jun 15, 2016):
The issue is that everytime the fuction in the builder getQuery is called, a new Query instance is created, by not allowing to disable the cache of the query at builder level I can't say that every query created by that builder has the query cache disabled.
In my specific case, since the Paginator allows to get a query builder as a parameter, it makes imposible to disable cache for the queries of the builder you are passing. In my code I pass the builder to some kind of data grid, which is generating multiple queries with same builder with slight differences, then passes the builder to the paginator. I was having some weird cache issues where getCountQuery()->getScalarResult was being cached with result 0. Because I have not been able to disable the cache for that specific query builder, instead I have had to create the query before and then pass it to the paginator with query cache disabled, resulting in disabling cache for every query generated within the data grid, which is not optimal. This is an example, I guess there might be other use cases where the ability to disable cache for every query generated by a query builder might be helpful.
I understand your concern over replicating the API of query on query builder. So, if you think it is not the way to go I completely understand. In that case tough, I would then suggest to remove the other cache options (lifetime, cacheable, cachemode and cacheregion) aswell because the behaviour as it is now was confusing when trying to find/fix the issue within my code.
Another idea of approach would be that the setCacheable function on the QueryBuilder makes to call both the setCacheable() and the useQueryCache() on the queries generated, but something in my goat says that would be wrong (because it is not consistent with the other functions). Code would be something like:
`
public function getQuery()
{
$parameters = clone $this->parameters;
$query = $this->_em->createQuery($this->getDQL())
->setParameters($parameters)
->setFirstResult($this->_firstResult)
->setMaxResults($this->_maxResults);
`
Whatever you decide, I offer myself to do change of code and open a pull request if it is needed. :)