开发者

Global model initialization in Kohana

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开发者_如何学JAVA 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜