This is not about a specific framework, but as a whole.

The database is mainly used in the model, so I connect it in the designer of the parent model, and then use it in the heirs.

Is that right or not? And if not correctly, why?


updated

I wrote a little incomprehensibly, I apologize

In general, I call the static class $ db = $ db :: ini (); in the constructor of the parent model, in the models I use $ db-> sql ('query');

Connection settings are registered in the config.

Those. when I call $ db :: ini () then a connection to the database takes place using pdo and the pointer is saved to the $ db variable available in the model's derived classes

I watched yii, then some dude on the type forum said everything in the controller needs to be done, and the connection itself in the bootstrap, where the car is happening and the files are connected.

I think that I am doing the right thing, connecting to the database when I need it, because this is the model for that - am I right?

  • "connect" it as? Are you connecting? If the question is not about frameworks, you need to remove them from the tags. In general, you yourself answered your question - look at how the database is connected in the frameworks and use the method that suits you best. - gridsane
  • DB is connected in the config for different env different connection. - vkovalchuk88

1 answer 1

Settings for the connection must be kept in the config, which in no case should not fall into the repository.

In modern frameworks (specified by you in the tags), the connection occurs at the moment when you need to take / change / delete something from the database. There is no need to invent anything, everything is thought up for you.

For example, we want to show an authorization form on the page (nothing more than a form). The page is loaded, and connected to the database does not occur because of uselessness. And when the user sends the form with the authorization data is connected to the database in order to find the user who is trying to authorize.