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

时间:2022-08-16 11:24:01

I'm using MVC pattern in my project,I want to implement MVC in my project perfectly without any loop holes.I've following situation my application,

我在我的项目中使用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
   }

The above code which I've shown is from view,I've fetched total records based on some conditions in controller and I've passed result to view,there again I got a situation where I want to communicate with model based on obtained records.I don't want to even call model layer function there,how can I avoid this,I'm using Kohana framework in my application.Thanks in advance for any help.

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

1 个解决方案

#1


1  

1) You need to have a model class for students:

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

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

2) Call the method from the controller and pass the result to the view:

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

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

#1


1  

1) You need to have a model class for students:

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

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

2) Call the method from the controller and pass the result to the view:

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

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