【问题标题】:PHP MVC: How to avoid calling model layer functions in viewsPHP MVC:如何避免在视图中调用模型层函数
【发布时间】:2012-08-25 13:04:55
【问题描述】:

我在我的项目中使用 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 框架。提前感谢您的帮助。

【问题讨论】:

  • 如果您使用 MVC 设计模式,那么视图应该从模型层请求信息。不,“模型层”不是“ORM”的不同名称。

标签: php model-view-controller kohana


【解决方案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);
// ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2014-03-06
    相关资源
    最近更新 更多