Kohana立即获得所有关系

时间:2011-02-20 12:13:35

标签: php orm kohana

是否可以立即获得所有对象关系?

目前,我在视图($this->post->comments->find_all())的循环中显示帖子评论但是它似乎不是最好的想法(那么缓存呢?)。

你通常如何解决这个问题?

修改 这是情况。当我显示最新帖子时,我有一个帖子控制器(总共约15000个,每页25个)。

在Post模型中,我设置了关系:has_many,包含注释,用户和选项。在同一个模型中,我得到了所有限制和偏移的帖子(用于分页)。

在视图中我有一个foreach循环,我正在显示帖子列表:

foreach($posts as $post)
{
    /// here in the view I have another loop for comments and options
}

现在的问题是:如何添加缓存?

1 个答案:

答案 0 :(得分:1)

这是我看到的正常用法。

如果您想使用缓存,请查看cached()函数。它不执行任何缓存,但返回一个可以序列化然后缓存的对象。用法是:

$results = Cache::instance()->get('item');

if ( ! $results)
{
    $results = $this->post->comments->find_all()->cached();

    $six_hours = 21600;

    // Save to the cache
    Cache::instance()->set('item', $results, $six_hours);
}

foreach($results as $comment)
{
    var_dump($comment);
}

请注意,文件缓存驱动程序会自动序列化数据,我假设其他驱动程序在内部做了一些魔法来存储缓存的对象。