我只是想绕过Kohana,所以如果我以错误的方式解决这个问题,请告诉我!
我想在输出中对结果进行分组。我在vanilla-PHP / PDO中这样做的方法是准备一个查询,然后在输出结果中执行它。但是我无法在Kohana(3.2.2)到达那里。
在这种情况下,在我的“条款”页面上,我想按“类型”对“条款”进行分组,每组条款用“类型”标题分隔。
我的'条款'控制器是(简化):
class Controller_Terms extends Controller {
public function action_index()
{
$view = View::factory('terms');
// types of term - for grouping terms together
$sql = 'SELECT DISTINCT Type
FROM Term';
$view->types = DB::query(Database::SELECT, $sql)->as_object()->execute();
// list of terms - executed separately for each type
$sql = 'SELECT TermId, Term
FROM Term
WHERE Type = :type';
$view->terms = DB::query(Database::SELECT, $sql)->as_object();
$this->response->body($view);
}
}
'条款'视图包括(简化):
<? foreach ($types as $type): ?>
<h2><?= $type->Type ?></h2>
<? $params = array(':type' => $type->Type);
$terms->parameters($params)->execute();
foreach ($terms as $term): ?>
<a href="term/<?= $term->TermId ?>"><?= $term->Term ?></a>
<? endforeach // term ?>
<? endforeach // type ?>
我将'type'标题设置为ok,但我没有在每种类型中获得任何术语。
任何建议都赞赏!
答案 0 :(得分:1)
execute()
returns一个特殊对象(Database_Result
),所以你需要这样的东西:
$items = $terms->parameters($params)->execute();
foreach ($items as $term): ?>
...