Whilst I have good experience with OOP, I'm fairly new to the concept of (H)MVC. I understand the basic concept of modular design that comes with HMVC, it's the great attraction of it. But one thing that has been bugging me, and will hopefully help me understand HMVC better is how do you handle the database connection?
I would assume that you would have a module, lets call it Database, that would initiate the connection. Now at the start of my script I would have something along the lines of:
$dbConn = new Database();
That would initiate the connection to the database. However, when I want to run a query I need to run something along the lines of:
$query = "my query string"; $result = $dbConn->query($query);
So I need to access the query function of the database object. Now as far as I can tell in HMVC I shouldn't directly access
$dbConn from within a model of another module. But almost every module will need to access
From my understanding this means that in the controller of all other modules, any time I need to run a query I would have to create a new instance of the Database module, passing in an array of arguments, telling Database which method to run and what arguments that method needs.
But then what if I need to make a call to the database half way through a method in a module's model? Quite often I will need to update a table, or select some data based on an if statement. Surely I shouldn't be moving that logic to the controller?
So that means I need to be able to access another module from within a model, which seems to break the rules of HMVC. So how do I implement a database module as this is confusing me?