Welcome Guest, Not a member yet? Register   Sign In
What is the correct way to load data from a databse once for use everywhere?

(This post was last modified: 02-11-2023, 03:06 PM by objecttothis. Edit Reason: Clarification )

I want to load into memory as an associative array all the rows from an app_config table so that the array can be accessed in Models, Views, Controllers, Helpers and Libraries and not need to do a database query each time.

This guy is doing it by creating a custom config, then populating the data in the constructor for that config: https://stackoverflow.com/questions/6463...gs-from-db

From what I can tell that will work, but unless I'm mistaken, every time config('foo')->bar gets called its going to call the constructor and pull all that data down from the database.  This defeats the purpose of trying to "load it into memory" once for use everywhere.

I was doing this through an event:

PHP Code:
$config = new Load_config();
Events::on('post_controller_constructor', [$config'load_config']); 

which is calling the load_config() function and that function populates the custom config I created.  The problem that I'm finding is that the data loaded into the custom config isn't persisting throughout.

Another idea that crossed my mind is loading the contents of the app_config table into the session data triggered by a post_controller_constructor event.  Any reason not to do that?

What is the best way to do what I'm trying to do here?

Messages In This Thread
What is the correct way to load data from a databse once for use everywhere? - by objecttothis - 02-11-2023, 08:32 AM

Theme © iAndrew 2016 - Forum software by © MyBB