问题
I have done some research about using a databasewrapper for my data. However, I read some posts where people claim you shouldn't use PDO for a databasewrapper because it already is one.
That may be so, but I am still convinced that it has a lot of benefits.
- You handle all your data actions (crud) in one class, not spread across your website files. So it's much easier to debug and handle errors.
- You can easilly change your class with another databaseclass.
- You don't have to repeat your code, just call the code in the databaseclass
- Optionally you can extend the class with for example logging, statistic tests, ...
Example:
<?php
class Database
{
public $connection;
private $host = "";
private $username = "";
private $password = "";
private $dbname = "";
public function __construct(){
$this->connection = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->username,$this->password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function insert($query, array $data){
$this->connection->prepare($query)->execute($data);
return $this->connection->lastInsertId();
}
public function update($query, array $data) {
$stmt = $this->executeQuery($query,$data);
return $stmt->rowCount();
}
public function delete($query, array $data) {
$stmt = $this->executeQuery($query,$data);
return $stmt->rowCount();
}
public function findOne($query, array $data = null){
$stmt = $this->executeQuery($query,$data);
return $stmt->fetchObject();
}
public function findMany($query, array $data = null){
$stmt = $this->executeQuery($query,$data);
return($stmt->fetchAll(PDO::FETCH_OBJ));
}
public function executeQuery($query,$data = null){
$stmt = $this->connection->prepare($query);
$stmt->execute($data);
return $stmt;
}
}
?>
Instead of constantly repeating all the steps for retrieving data, you can call get() and pass a query and some optional parameters. This is a lot more efficiënt than everytime open a connection, perform 3 lines of code and close it.
$db->get("select * from user where id = ?",array(1));
回答1:
It is said because most attempts to create a PDO wrapper are indeed helpless and make things worse than with raw PDO.
Also premises, one is writing their wrapper based upon, are mostly wrong.
Let's take yours:
You handle all your data in one class, not spread across your website files.
Quite vague statement, with no particular meaning. Surely you handle your data all across the application but using not raw PDO but using your own class.
You can easilly change your class with another databaseclass.
But a delusion. You can't actually stick to your two helper methods - sometimes you need to use raw PDO instance.
You don't have to repeat your code, just call the code in the databaseclass
this one is quite true. But not that useful as with any other API.
Optionally you can extend the class with for example logging, statistic tests
This is a proper one - no objections.
This is a lot more efficiënt than everytime open a connection, perform 3 lines of code and close it.
this is a false one. Nobody asks you everytime open a connection, perform 3 lines of code and close it. Even with raw PDO connection is opened only once.
However, your implementation is quite good. It doesn't add too much to raw PSO (actually, it shorten repetitions by just one line only) but still being sensible. So - I'd call this approach rather successful.
Some suggestions:
- there is indeed no point in storing database credentials as class properties. They needed in constructor only and nowhere else
- Why not to create a connection right in the constructor, without the need for extra method?
- public function getOne() would be indispensable addition to your set.
- You need to make $connection public - at least until you make all the methods available from both PDO and PDOstatement
回答2:
'Overkill' is subjective. Thats for you to decide for your particular case. Performance-wise I doubt you'd notice any difference.
There are some possible benefits of doing something like this. For example, you can embed query profiling and application logging into the class. And, as you point out, you have the option of structuring the queries in a way thats easiest for you to work with and maintain.
On the other hand, the code will be less portable if it relies on your wrapper class. You cant then take that code and use it with code that relies on PDO. You might get around some of those limitations by extending the PDO class, rather than creating a wrapper for it.
来源:https://stackoverflow.com/questions/20664450/is-a-pdo-wrapper-really-overkill