【发布时间】:2013-10-30 13:23:52
【问题描述】:
我有扩展 ORM 的 Model_Group。
我的 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 模型中的一个函数。你可以在你想要的每个控制器中调用该函数。 -
这是一个有趣的观点。所以你的意思是我会通过
$result = $orm_group->get_by_type($type);来调用它?
标签: orm controller kohana-3 hmvc kohana-orm