我在查询中遇到了一个问题。请帮我解决问题(我是Kohana Framework的新手)
$posts->select(array(DB::expr('( SELECT COUNT(id_visit) FROM `oc2_visits` WHERE `oc2_post`.`id_post` = `oc2_visits`.`id_ad` AND `oc2_visits`.controller = "Blog" GROUP BY `oc2_visits`.`id_ad`)'), 'hits'));
//we sort all ads with few parameters
$posts = $posts->order_by('created','desc')
->limit($pagination->items_per_page)
->offset($pagination->offset)
->limit(Theme::get('num_home_blog_posts', 4))->cached()
->find_all();
正如您所见,'hits'是在DB:expr()中设置的属性。在我看来,我正试图访问$ posts->点击;属性。然后出现hits属性的问题不存在。
图片附后,请帮助我不是kohana框架的专家。
答案 0 :(得分:0)
尝试这种非ORM方法:
$query = DB::select(array(DB::Expr('COUNT(v.id_visit)'), 'hits'))
->from(array('oc2_visits', 'v'))
->join(array('oc2_post', 'p'), 'INNER')
->on('p.id_post', '=', 'v.id_ad')
->where('v.controller', '=', 'Blog')
->group_by('v.id_ad')
->order_by('v.created','desc')
->limit($pagination->items_per_page) //why 2 limit statements? Oversight?
->offset($pagination->offset)
->limit(Theme::get('num_home_blog_posts', 4));
$posts = $query->execute();
//Or if you want objects use:
$posts = $query->as_object()->execute();