1
I am studying MVC, I created the abstract Model class to serve as a "template" for the others who can inherit it. I would like an evaluation of the code as, positive and negative points, how best to improve it, etc.
abstract class Model {
private $sql;
protected function setSql($sql_query) {
return isset($sql_query) ? $this -> sql = $sql_query : false;
}
public function getAll() {
$query = $this -> db -> prepare($this -> sql);
try {
$query -> execute();
return $query -> fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
die($e -> getMessage());
}
}
public function getById($id) {
$query = $this -> db -> prepare($this -> sql);
$query -> bindValue(1, $id);
try {
$query -> execute();
return $query -> fetch(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
die($e -> getMessage());
}
}
public function getBySearch($search_term) {
$query = $this -> db -> prepare($this -> sql);
$query -> bindValue(1, $search_term);
try {
$query -> execute();
return $query -> fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
die($e -> getMessage());
}
}
public function deleteById($id) {/*será implementada*/}
public function updateById($id) {/*será implementada*/}
public function insert() {/*será implementada*/ }
}
And this would be the call in a daughter class:
public function getUserById($id) {
$this -> setSql("SELECT * FROM XXX WHERE id = ?");
return $this -> getById($id);
This question is being discussed from the Meta: http://meta.pt.stackoverflow.com/questions/1510/devemos-aceitar-pedidos-improvementof c%C3%B3digo
– user7261
I recommend using Doctrine. It’s an excellent tool for abstraction and relationship mapping. Check out the documentation
– William Urbano