I'm studying MVC, I created the abstract Model class to serve as a "template" for the others that can inherit it. I would like an evaluation of the code as, positives and negatives, 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 child class:
public function getUserById($id) {
$this -> setSql("SELECT * FROM XXX WHERE id = ?");
return $this -> getById($id);