Doctrine Query Cache Prevention in Symfony

In my Symfony / Doctrine application, I have a query that orders RANDOM (). I call the same method several times, but it looks like the query result is cached.

Here is my corresponding code:

$query = $table->createQuery('p') ->select('p.*, RANDOM() as rnd') ->orderBy('rnd') ->limit(1) ->useQueryCache(null) ->useResultCache(null); $result = $query->fetchOne(); 

Unfortunately, the same record is returned every time, regardless of the fact that I pass null both useQueryCache and useResultCache . I tried using false instead of null , but that didn't work either. Finally, I also tried calling both setResultCacheLifeSpan(0) and setResultCacheLifeSpan(-1) , but not a single call made a difference.

Any insight on how to prevent caching, as I want every time I call this method, I select a different random string?

Edit: I also tried calling clearResultCache() , but it only ended up causing the error: "The result cache driver is not initialized."

Edit 2: As requested here, SQL is generated by calling $query->getSqlQuery() :

 SELECT c.id AS c__id, c.name AS c__name, c.image_url AS c__image_url, c.level AS c__level, c.created_at AS c__created_at, c.updated_at AS c__updated_at, RANDOM() AS c__0 FROM cards c ORDER BY c__0 LIMIT 1 
+8
caching symfony1 doctrine
Feb 07 2018-11-11T00:
source share
2 answers

Turns out I'm an idiot. I tried to simplify my query for this question, and yet I did not understand the true reason. I had a call to where() and andWhere() , and a combination of conditions led to matching only one possible record. Thank you for taking the time to answer, everyone, sorry, wasted your time!

+4
Feb 08 '11 at 23:10
source share
— -

Doctrine also caches the objects you created in the same / script run request.

For example:

 $order = new Order(); $order->save(); sleep(10); // Edit this record in de DB in another procces. $q = new Doctrine_Query(); $result = $q->select() ->from('Order o') ->where('o.id = '.$order->id); $order = $result->getFirst(); print_r($order->toArray()); 

print_r will not contain the changes you made during sleep.

The following code will delete a cache of this type:

 $manager = Doctrine_Manager::getInstance(); $connection = $manager->getCurrentConnection(); $tables = $connection->getTables(); foreach ( $tables as $table ) { $table->clear(); } 

PS: this answer was added because I found this topic trying to solve the above problem.

+2
Mar 31 2018-11-11T00:
source share



All Articles