在Symfony中预防Doctrine的查询缓存 [英] Preventing Doctrine's query cache in Symfony

查看:369
本文介绍了在Symfony中预防Doctrine的查询缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Symfony / Doctrine应用程序中,我有一个由RANDOM()命令的查询。我打电话给同样的方法几次,但看起来查询的结果正在缓存。



这是我的相关代码:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $& $ b - > orderBy('rnd')
- > limit(1)
- > useQueryCache(null)
- > useResultCache(null);
$ result = $ query-> fetchOne();

不幸的是,每次都会返回相同的记录,无论我通过 null 同时使用 useQueryCache useResultCache 。我尝试使用 false 而不是 null ,但这也没有。最后,我还尝试调用 setResultCacheLifeSpan(0) setResultCacheLifeSpan(-1),但是两个调用都不同



有关如何防止缓存的任何洞察,因为我希望每次调用此方法时都会选择不同的随机行?



编辑:我也尝试调用 clearResultCache(),但最终导致错误,说明:结果缓存驱动程序不初始化。



编辑2:根据要求,这里是通过调用 $ 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


解决方案

原来我是一个蠢货。我试图简化我对这个问题的查询,在这样做时,我没有捕捉到真正的原因。我有一个,其中()和Where()调用,条件的组合导致只有一个可能的记录匹配。感谢您抽出宝贵的时间回复,每个人都很遗憾浪费您的时间!


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

Here's my relevant 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 me passing null to both useQueryCache and useResultCache. I tried using false instead of null, but that didn't work either. Lastly, I also tried calling both setResultCacheLifeSpan(0) and setResultCacheLifeSpan(-1), but neither call made a difference.

Any insight on how to prevent caching since I want a different random row to be selected each time I call this method?

Edit: I also tried calling clearResultCache(), but that just ended up causing an error stating: "Result Cache driver not initialized".

Edit 2: As requested, here's the SQL 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

解决方案

It turns out I'm a moron. I tried to simplify my query for this question, and in doing so, I didn't capture the true cause. I had a where() and andWhere() call, and the combination of conditions resulted in only one possible record being matched. Thanks for taking the time to respond, everyone, sorry to have wasted your time!

这篇关于在Symfony中预防Doctrine的查询缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆