2011-07-20 40 views
0

我有一个模型从我的数据库中提取一个我希望能够在我的视图中显示的列表。将模型中的值传递给Kohana中的视图3

这个模型看起来是这样的:

Class Model_services extends Model 
{ 
    public function get_services_list() { 
    $result = DB::select('*')->from('services') 
      ->execute() 
      ->as_array(); 
      return $result; 
    } 
} 

我的控制器看起来是这样的:

public function action_index() 
{ 
    $this->template->title = "services"; 
    $this->template->header = View::factory('header'); 
    $services     = Model::factory('services'); 
    $this->template->content = View::factory('services') 
             ->bind('result',$result) 
    $this->template->footer = View::factory('footer');   
} 

如何呈现在视图从模型中的变量?

回答

0

在如下services.php使用代码;)

foreach($result as $item) { 
    echo Debug::vars($item); 
    // print_r($item); //alternatively you can try this also, if Debug::vars() causes pain 
} 
+0

感谢Kowser快速回复,我使用的模板控制器,现在它给我这个错误“ErrorException [2]:htmlspecialchars()[function.htmlspecialchars]:无效的多字节序列在参数〜SYSPATH \ classes \ kohana \ debug.php [311]“我仍然不知道我做错了什么 – user731144

+0

从我的经验,我可以理解的事情$ item包含了一些具有unicode数据的东西,从而使该unicode数据成为多字节序列。你可以尝试使用print_r($ item)而不是Debug :: vars();作为你的 – Kowser

+0

,它只是空,它更好地调查你的模型。至少它不应该是空的,这是我的理解。 – Kowser

2

您还没有实际上被称为模型法或传递的变量从控制器类的观点。

更改此:

$services = Model::factory('services'); 
$this->template->content = View::factory('services') 
            ->bind('result',$result); 

要这样:

$services = Model::factory('services')->get_services_list(); 
$this->template->content = View::factory('services') 
            ->bind('result', $services); 

的位置变化是:

  • 调用用于从数据库中提取相关行的方法。
  • 使用$services变量并将其绑定到$result,您将在视图中使用它。

在您的视图中,您可以在参考$result变量时提取值。

要查看你从模型中得到了什么,在你看来测试:

echo Debug::vars($result); 
相关问题