关联时的cache

假设 A hasmany B,我是lazy loading

现如下代码




foreach (A as a) {

foreach(a->cache(1800)->B(array('limit'=>12)) as <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' /> {

...结果取出来的记录都是相同的

}

}



请问是不是不能这么用?

你这个不是eager loading。你可以参考http://www.yiiframework.com/doc/guide/1.1/en/caching.data的最后一个例子。

如果你是同时执行两条SQL查询,你需要设置一个参数,cache(1800,NULL,2),

2改为你自己需要同时执行的SQL数量,如果没有设置,将只会缓存第一条SQL查询,

按照官方文档说明:

if we use query caching as follows, only the first DB query will be cached:

$posts = Post::model()->cache(1000, $dependency)->with(‘comments’)->findAll(array( ‘limit’=>20, ));

In order to cache both DB queries, we need supply the extra parameter indicating how many DB queries we want to cache next:

$posts = Post::model()->cache(1000, $dependency, 2)->with(‘comments’)->findAll(array( ‘limit’=>20, ));

http://www.yiiframework.com/doc/guide/1.1/en/caching.data#query-caching

我本来是想


A::model()->cache(1800, $d, 2)->with(array('B'=>array('limit'=>20)))->findAll();

但发现对B限制不了limit,

所以我又改成了


$as = A::model()->cache(1800)->findAll();

foreach ($as as $a) {

$bs = $a->cache(1800)->B(array('limit'=>20));

foreach ...

}

请问想实现我这种要求,该怎么写呀

你这样应该是可以的。仔细查看一下执行的SQL是否都对了。

不行呀,现在我这种情况的问题是

$bs里的所有数据,和第一次foreach出来是一样的,并没有随着$a的foreach改变而改变

只要把$bs = $a->cache(1800)->B(array(‘limit’=>20));

里的cache去,就正常了

对不起,我太着急了,刚刚调试又是好的,麻烦强了。

我再试试

强,今天试了一下,还是老样子。数据是重复的。

详情请见:http://ra.0577home.net/

最下面部分