开发者

Codeigniter: when to use a model vs library?

开发者 https://www.devze.com 2023-01-05 03:18 出处:网络
I\'ve starting using Codeigniter for a project recently (few months ago) but its getting a lit开发者_如何转开发tle out of hand with a bunch of models that need to interact with each other and I was wo

I've starting using Codeigniter for a project recently (few months ago) but its getting a lit开发者_如何转开发tle out of hand with a bunch of models that need to interact with each other and I was wondering if I should be creating a library instead?

In my case, I have a user action that happens when you win a game and it would get logged in my user_model but I also want it to get put in my events_model?

Should something like this that affects multiple models become a library?

I know it should NOT be in the controller because I'd have to reuse this trigger in multiple controllers (might not make sense for the provided example but does for my application).


Libraries are meant to provide reusable functionality for your application, while models in the MVC architecture represent the data logic in or the actual data used by your application.

So to answer your question, you can implement a library that handles updating of multiple models. Just ensure that things like database inserts are handled by your models themselves, as that is business logic more than application logic.


You could leave your models modularised as they are and write your logic in libraries, this way you can access mutilple models though a single function in a library, without making your controllers untidy by loading multiple models and having none-interface orientated logic mingled in with interface logic.

0

精彩评论

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