0

我只是想讓我的頭在Kohana附近,所以如果我以錯誤的方式解決這個問題,請告訴我!Kohana參數化查詢在查看範圍內的執行

我想在我的輸出中分組結果。我在vanilla-PHP/PDO中這樣做的方式是準備一個查詢,然後在輸出結果中執行它。不過,我不能在Kohana(3.2.2)中進入。

在這種情況下,在我的'terms'頁面上,我想按'type'將'terms'分組,每組術語用'type'標題分隔。

我的 '術語' 控制器(簡體):

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 ?> 

我得到的 '類型' 頭好,但我不要在每種類型中獲得任何條款。

任何建議表示讚賞!

回答

1

​​returns的特殊對象(Database_Result),所以你需要的東西是這樣的:

$items = $terms->parameters($params)->execute(); 
foreach ($items as $term): ?> 
... 
+0

正是我需要的,謝謝 - 作品一種享受! – ChrisV

+1

我認爲你不應該從視圖執行查詢。視圖用於表示邏輯,而不用於業務邏輯。另外,你可以用一個查詢來替換'foreach'(select * from term where type in ... order by type)。 – biakaveron