Welcome Guest, Not a member yet? Register   Sign In
Using libraries alongside models
#6

In my opinion, it is better to load/call a model from a library than to call the database from your library.

One example I can think of in Bonfire is a Settings library which allows settings to be stored in the database but also allows settings to be retrieved from CI config files (if they do not exist in the database). So, it uses a settings model to access the database, but if the value requested is not found, it uses the config_item() helper function to retrieve the setting from a config file (and caches the result regardless of the original location so it can be retrieved later without making an additional call to the database or the file).

Then you simply determine (more or less on a case-by-case basis) whether you should only use the library to access the model, or if the model should also be directly accessed by a controller in some situations.

In the end, only a model should access the database. If a library needs access to data, it should either be provided to the library by the caller or the library should load/use a model which can give it the required data. Ultimately, if the data is provided to the library (or the model is injected) it will usually provide the best long-term maintainability and potential for reuse for that library, but this isn't always the most practical way to develop your application.
Reply


Messages In This Thread
Using libraries alongside models - by mdixon18 - 03-29-2015, 04:06 PM
RE: Using libraries alongside models - by mwhitney - 03-30-2015, 09:41 AM
RE: Using libraries alongside models - by RWCH - 04-03-2015, 07:17 AM
RE: Using libraries alongside models - by gadelat - 04-03-2015, 08:31 AM
RE: Using libraries alongside models - by RWCH - 04-07-2015, 03:44 AM
RE: Using libraries alongside models - by RWCH - 04-07-2015, 03:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB