PHP MVC:如何避免在视图中调用模型层函数

时间:2012-08-25 13:04:56

标签: php model-view-controller kohana

我在我的项目中使用MVC模式,我想在我的项目中完美地实现MVC而没有任何循环漏洞。我已经了解了我的应用程序,

  foreach($std_results as $std_result)
   {
      $std_name = ORM::factory('students')->where('id',$std_result->hall_ticket_number);//I want to avoid this     
       //other stuff follows from here
   }

我已经显示的上面的代码来自视图,我根据控制器中的某些条件获取了总记录,并且我已经将结果传递给了查看,我又遇到了一种情况,我希望与基于模型的通信获得的记录。我甚至不想在那里调用模型层函数,我怎么能避免这种情况,我在我的应用程序中使用Kohana框架。提前感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

1)您需要为学生准备一个模型课程:

class Student extends ORM
{
   public function your_function()
   {
      // Do the DB stuff here
   }
}

2)从控制器调用方法并将结果传递给视图:

// ...
$std_results = ORM::factory('student')->your_function();
// ...
$view->bind('std_results', $std_results);
// ...