Kohana 中的全局模型初始化
我是 Kohana 新手,遇到了以下问题:
我有 3 个模型,模型 1、模型 2、模型 3。每次我想使用其中一个模型中的方法(在不同的控制器方法/模型中)时,我都必须 $model1 = New Model_Model1();
在控制器中,我可以执行 public $model1
并在构造函数中创建模型,这样就可以了。然而,在模型中,我设法获得模型初始化的无限循环。
在 CodeIgniter 中,这很简单:我可以自动加载模型。如何使我的模型在 Kohana 中全局可访问,以便我可以从任何地方 $this->model1->dosth();
,而不必担心创建同一模型的多个实例?
I'm new to Kohana and bumped into the following problem:
I have 3 models, model1, model2, model3. Every time I want to use a method from one of the models (in a different controller method / model) I have to$model1 = New Model_Model1();
In a controller, I can do a public $model1
and create the model in the constructor, that's OK. In a model, however, I managed to get an infinite loop of model initialization.
In CodeIgniter, it's easy: I can autoload models. How do I make my models globally accessible in Kohana so that I could $this->model1->dosth();
from anywhere, without worrying about creating multiple instances of the same model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您可以在 CodeIgniter 中执行
$this->model->method()
的唯一原因是因为它有一个超级对象;在 Kohana 中,您可以在模型中创建一个实例方法,然后只需调用 Model::instance() ...如果您需要全局实例。如果您只需要在控制器中访问它们,那么您可以覆盖默认控制器,在构造函数中加载模型并在子控制器中使用它们。
这完全取决于您的实际情况。加载新的模型实例并不总是坏事。
First of all, the only reason you can do
$this->model->method()
in CodeIgniter is because it has a super object;In Kohana you can create an instance method in your model and simply call Model::instance() ... if you need a global instance. If you need to access them in controllers only then you can override the default controller, load your models in the constructor and use them in your child controllers.
It all depends on your situation really. Loading a new model instance isn't always a bad thing.