Creating a database wrapper not a good idea?
I know working with an orm would be better and I am planning to use it in
the future. But for now, Im working with a structure like this:
Class Arcticle with title and date Class DataArticle for the database
actions So Im not performing my database actions in my Article class but
in a separate Data class.
Now, in all my Data.. classes I used code to perform database actions like
this:
public function getArticle($id){
$query = "SELECT title,date from articles where id = ?";
if ($stmt = $this->database->getConnection()->prepare($query)) {
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->bind_result($title,$date);
$stmt->store_result();
$stmt->fetch();
if(($stmt->num_rows) == 1){
$article = new Article();
$article->title = $title;
$article->date = $date;
$stmt->close();
return $article;
}else{
$stmt->close();
return null;
}
}else{
throw new Exception($this->database->getConnection()->error);
}
}
But working this way means that in every function in my data classes I
would connect, perform a statement and throw errors. That is a lot of
repeated code which could be centralized using a wrapper.
Now I am following the advice (Throw an exception in a function or how to
do descent error handling) on creating a database wrapper/handler to
perform all the database stuff so it is all centralized in one class which
makes it easier to maintain.
So I created this class to start using PDO:
<?php
class DatabasePDO
{
private $connection;
private $host = "";
private $username = "";
private $password = "";
private $dbname = "";
public function openConnection(){
$this->connection = new
PDO("mysql:host=$this->host;dbname=$this->dbname",$this->username,$this->password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
}
public function getConnection(){
return $this->connection;
}
public function closeConnection(){
$this->connection = null;
}
public function insert($query, array $data){
$this->connection->prepare($query)->execute($data);
return $this->connection->lastInsertId();
}
public function update($query, array $data) {
$stmt = $this->connection->prepare($query);
$stmt->execute($data);
return $stmt->rowCount();
}
public function delete($query, array $data) {
$stmt = $this->connection->prepare($query);
$stmt->execute($data);
return $stmt->rowCount();
}
public function findOne($query, array $data = null){
$sth = $this->connection->prepare($query);
if($data != null){
$sth->execute($data);
}else{
$sth->execute();
}
if($sth->rowCount() == 1){
return $sth->fetchObject();
}else{
return null;
}
}
public function find($query, array $data = null){
$sth = $this->connection->prepare($query);
if($data != null){
$sth->execute($data);
}else{
$sth->execute();
}
if($sth->rowCount() > 0){
while($res = $sth->fetchObject()){
$results[] = $res;
}
return $results;
}else{
return null;
}
}
}
?>
But when reading some articles I found out that this isn't good practice
because PDO already is a database wrapper.
However, by code is much more readable as before. Now it's just
public function getArticle($id){
$article = $this->database->find("select name, date from articles
?",array($id));
$article = new article($article->name, $f->date);
return $article;
}
this code is much shorter and all the database logic is handled in the PDO
wrapper class, otherwise I would have to repeat the code of the wrapper in
every function and my code would be in a lot of places instead of one
wrapper.
So is there a better way to use my code or is it a good way I'm using it.
No comments:
Post a Comment