好的,我想通了。我通过创建classes/orm.php
延长了ORM驱动程序,它包含以下内容:
<?php defined('SYSPATH') or die('No direct access allowed.');
class ORM extends Kohana_ORM {
public function add_subquery($query) {
$this->_db_pending[] = array(
'name' => 'select',
'args' => array(DB::expr($query)),
);
return $this;
}
}
然后,在我的ORM电话,我做了以下内容:
$questions = ORM::factory('question')
->add_subquery('(SELECT COUNT(answer_id) FROM user_question_answers WHERE answer_question_id = question_id) as answer_count')
->where('question_user_id', '=', $this->current_user->id)
->find_all();
我还是搞清楚所有的插件和Kohana的出局,我很担心,使用DB::expr()
可能造成这应该与用户提交的数据使用的安全风险。但我不会用它从用户任何东西,所以我确定现在。
如果有人有更好的解决方案,我会很乐意看到你将如何解决这个问题。
来源
2012-07-30 02:13:50
jdp
为什么你不想添加分开的计数查询?缓存结果和下降缓存,当你有该用户的新的答案。 – biakaveron 2012-08-05 10:29:14