Jump to content

How Do I Handle The Multiple Database Connects And Requests With Mvc?


Recommended Posts

I just started a MVC style web application and I have a model which has a sole job of creating a connection to the database. I also have more models which connect to various tables to do various jobs but they're separate files in the model class. How do I use the connection in the database_connection_model in all the other model classes? Do I pass it into every model I use from the controller or is there some other way? I've considered just connecting in the model constructor but then where do I close the connection? Please help. Thanks.

If I get you correctly, you aren't talking about connecting to different databases but using the same connection multiple times throughout the application?I would look in to building a static db class.
Same database but multiple models which connect to various tables in that database. I could either:

a) connect to the database inside each separate model.

b) connect to the database in the controller and pass the returned connection into each model used

c) extend the database_connect model in every other model which uses it (I couldn't figure this out)Which is best?

Edited by DeX

Dependency injection is the right answer. The others don't make sense logically (extending your db class) or methodologically (static class).

So according to this website: [http://www.potstuck.com/2009/01/08/php-dependency-injection/], does that mean I would need a database setter method in every new model file I make and also a databaseConfig setter? That seems like a lot of unnecessary and redundant code.

What KevinM1 was suggesting is that you pass your dependency (A database connection object) into the table specific models. Since those classes won't work without the database connection, it probably makes sense to pass it as a constructor parameter:

 

$foomodel = new FooModel($db);

 

Now one way to make this work well is to bake in a registry object into your controller class, where you will be making models. Or you could make it part of the controller class, perhaps as a base class, so that it is automatically something you can get access to in any controller, ala:

 

// In controller
$fooModel = new FooModel($this->getDb());

 

Let's say that all your controllers implement a base controller class -- then you simply have your getDB() check to see if it has a database connection object stored, or if not, creates a new one.

What KevinM1 was suggesting is that you pass your dependency (A database connection object) into the table specific models. Since those classes won't work without the database connection, it probably makes sense to pass it as a constructor parameter:

 

$foomodel = new FooModel($db);

 

Now one way to make this work well is to bake in a registry object into your controller class, where you will be making models. Or you could make it part of the controller class, perhaps as a base class, so that it is automatically something you can get access to in any controller, ala:

 

// In controller
$fooModel = new FooModel($this->getDb());

 

Let's say that all your controllers implement a base controller class -- then you simply have your getDB() check to see if it has a database connection object stored, or if not, creates a new one.

 

Okay, so this would be option B from what I posted earlier?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.