开发者

How would I change this Query builder from Kohana 2.4 to Kohana 3?

I have had this website built for a few months and I am just getting on Kohana 3. I'd just like to convert this K2.4 query builder to K3 query builder.

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')
        ->joi开发者_如何转开发n('images')->on('images.post_id', '=', 'posts.id')
        ->group_by(array('posts.id'))
        ->order_by('posts.id', 'DESC')
        ->limit($limit)
        ->offset($offset)
        ->execute();


The only change you need to make, is drop the surrounding array from the DB::select(), and for the aliased field, use an array

The query builder in Kohana3 accepts any number of arguments, see 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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜