【问题标题】:How would I change this Query builder from Kohana 2.4 to Kohana 3?如何将此查询构建器从 Kohana 2.4 更改为 Kohana 3?
【发布时间】:2011-01-11 11:56:36
【问题描述】:

我已经建立这个网站几个月了,我刚刚开始使用 Kohana 3。我只想将此 K2.4 查询构建器转换为 K3 查询构建器。

return DB::select(array('posts.id', 'posts.created', 'posts.uri', 'posts.price', 'posts.description', 'posts.title',
                'image_count' => db::expr('COUNT(images.id)')))
        ->from('posts')
        ->join('images')->on('images.post_id', '=', 'posts.id')
        ->group_by(array('posts.id'))
        ->order_by('posts.id', 'DESC')
        ->limit($limit)
        ->offset($offset)
        ->execute();

【问题讨论】:

    标签: kohana kohana-3


    【解决方案1】:

    您需要做的唯一更改是从 DB::select() 中删除周围的数组,对于别名字段,请使用数组

    Kohana3 中的查询构建器接受任意数量的参数,请参阅http://kohanaframework.org/guide/database/query/builder

    return DB::select('posts.id', 'posts.created', 'posts.uri', 
                'posts.price', 'posts.description', 'posts.title',
                array('COUNT("images.id")', 'image_count'))
        ->from('posts')
        ->join('images')->on('images.post_id', '=', 'posts.id')
        ->group_by(array('posts.id'))
        ->order_by('posts.id', 'DESC')
        ->limit($limit)
        ->offset($offset)
        ->execute();
    

    【讨论】:

    • 我实际上是在一小时前得到的。我只是在等待有人给出答案,所以我可以接受;)
    • 呵呵,一小时前我才看到q
    • 更好的是,使用array('COUNT("images.id")', 'image_count') 以便您的列正确转义。
    • 我已更改代码以允许正确转义列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    相关资源
    最近更新 更多