What is the most appropriate way to instantiate a database in an MVC pattern

1

I'm studying OOP and a little bit about the MVC structure, and I'm not sure where to create and instantiate the database to be used in the entire application, I thought how Model relates to data processing would be the best place to create the scope of the class, and as only the classes that extended the Model would also use the database, I did the following, for example:

Class Model {

    private static $db = null;
    private static $dbHost = 'localhost';
    private static $dbName = 'mini';
    private static $dbUser = 'root';
    private static $dbPass = '';
    private static $dbCharset = 'utf8';

    protected function __construct() {
        $this -> setConnection();
    }

    protected function getConnection() {
        return self::$db;
    }

    private function setConnection() {

        if (self::$db == null) {

            $op = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);
            self::$db = new PDO('mysql:host=' . self::$dbHost . ';dbname=' . self::$dbName . ';charset=' . self::$dbCharset, self::$dbUser, self::$dbPass, $op);
            return self::$db;
        }
    }

}

class userModel extends Model {

    function __construct() {
        parent::__construct();
    }

    public function getUser() {

        $getAll = $this -> getConnection() -> prepare("SELECT * FROM usuarios");
        $getAll -> execute();
        $test = $getAll -> fetch(PDO::FETCH_ASSOC);
        return $test;
    }

}

And the call, in a controller, for example would be as follows:

$userModel = new userModel;
$getUsers = $userModel -> getUser();

Is this a good practice? and if not, why? as I am new to this, some concepts can be very advanced still, so I ask you to explain in a simple and gradual way.

    
asked by anonymous 14.05.2017 / 03:13

1 answer

2

Yes, it is a good practice, even this template is a standard known as DAO (Data Access Object) , not in its entirety, but what you have done there has many characteristics of it.

Some considerations of what you can improve:

Where you put the connection data:

private static $db = null;
private static $dbHost = 'localhost';
private static $dbName = 'mini';
private static $dbUser = 'root';
private static $dbPass = '';
private static $dbCharset = 'utf8';

Never put sensitive data from connections into codes. What I advise you to do is to create a .env file in ini format (you can even call it from .env.ini and use the parse_ini_files function of PHP to read it. In this file, place your connection settings and database.

Now for my second consideration, DAO objects, that is, your entities models, for example, you have the user class that has your model, the user class would have its UsuarioDAO which would be the model that would do access to the database. This makes it necessary to use a connection manager class, a pool of connections as I call it.

This template uses the standard singleton to return only a single instance that is connected to the database so that all DAO objects use the same connection and do not open new ones over time.

I advise you to use the connection manager model (a new class) that will be responsible for serving a connection to all objects that connect to the database.

The last comment I have would be to not have a method that leaves the query open, as you did here:

   $getAll = $this -> getConnection() -> prepare("SELECT * FROM usuarios");

Notice the following, you already gave a name to this action: getAll , ie a DAO object should contain all the actions that are possible for it, in this case you should have some of the type: getAll, getOne , getByName, getById, etc ...

It's a bad practice to leave the query open, because even if you do, the sense in creating object models is totally lost.

    
14.05.2017 / 05:36