2
I’m doing an MVC application for study purposes, here the structure:
Sketch of my Model class that will be extended by the child classes.
<?php
abstract class Model {
protected $db;
protected $table;
function __construct(PDO $db, $table) {
$this -> db = $db;
$this -> table = $table;
}
public function getAll($where = null) {
$query = "SELECT * FROM {$this->table}";
if ($where)
$query .= "WHERE {$where}";
$query = $this -> db -> prepare($query);
try {
$query -> execute();
return $query -> fetchAll();
} catch(PDOException $e) {
die($e -> getMessage());
}
}
public function getById($id) {
$query = $this -> db -> prepare("SELECT * FROM {$this->table} WHERE id = :id");
$query -> bindParam(':id', $id);
try {
$query -> execute();
return $query -> fetch();
} catch(PDOException $e) {
die($e -> getMessage());
}
}
In the controller class I open the connection:
class Controller {
function __construct() {
}
public function openDB() {
$options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);
$this -> db = new PDO(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=' . DB_CHARSET, DB_USER, DB_PASS, $options);
return $this -> db;
}
}
Example of a controller: (Taking into account that I already inherited the Controller class and includes the class files)
function index() {
$db = $this -> openDB(); /*conexão é aberta*/
$testeModel = new testeModel($db);
$getAll= $testModel-> getAll();
}
When I use the function Getbyid or Getall I understand a certain lag, even if the application is in localhost that lag is considerable... and I have in mind that these are certainly not the best practice for this, I would like some suggestions that can improve the performance of the application/communication with the bank, but nothing too complex, since I am in a learning process, I thank you.
I saw no error... but the truth is that mysqli has more performance than Pdo... it is little but has... the problem of using mysqli, if it is a problem, is that you can only use db mysql...
– Andrei Coelho
The problem seems to be your environment, if possible test the code in another environment.
– rray
As @rray said, your code is correct and there is nothing that can improve performance significantly, the slowness is in your development environment, maybe some configuration in the Framework, database, or something like that.
– Edson Horacio Junior
Good afternoon, I recommend you make more intuitive titles to the "problem". Edited, if you disagree make a rollback.
– Guilherme Nascimento
Just like the previous comments, there is nothing wrong with your code. However, there are some things that should be taken into account when low application performance is perceived. In your case, the first thing to check is if your tables are with adequate indexes. The second is that your webserver+PHP has enough memory to process the data. Reduce the possibilities from the easiest to check to the most difficult. Start with the environment. Test codes without BD access to ensure that the problem is not in the environment.
– Marcos Regis
I think it is the server, I use Linux a few times and I have tested this application, it runs almost instantaneous, already in windows I use Easyphp, I will look for something to implement it, thanks for the comments!
– Thiago