我有扩展ORM的Model_Group。Kohana - 在控制器之间传递ORM对象的最佳方式?
我有Controller_Group是得到一个新的ORM:
public function before()
{
global $orm_group;
$orm_group = ORM::factory('Group');
}
...它有一个用它来获取数据的不同子集,如各种方法......
public function action_get_by_type()
{
global $orm_group;
$type = $this->request->param('type');
$result = $orm_group->where('type', '=', $type)->find_all();
}
然后我有另一个控制器(在一个单独的模块中),我想用它来操作对象并调用相关的视图。我们称之为Controller_Pages。
$orm_object = // Get the $result from Controller_Group somehow!
$this->template->content = View::factory('page1')
->set('orm_object', $orm_object)
什么是将ORM对象从Controller_Group传递到Controller_Pages的最佳方法是什么?这是一个好主意吗?如果没有,为什么不,以及有什么更好的方法呢?
将它们分离到不同的控制器中的原因是因为我希望能够从其他模块中重新使用Controller_Group中的方法。每个模块可能都想以不同的方式处理对象。
我觉得功能'action_get_by_type'应该是你的ORM模型中的一个函数。比你可以在每个你想要的控制器中调用该函数。 – Manuras
这是一个有趣的事情。所以你的意思是我会通过执行'$ result = $ orm_group-> get_by_type($ type);''来调用它。 – SigmaSteve