pdo包装器真的过大了吗? [英] Is a pdo wrapper really overkill?

查看:82
本文介绍了pdo包装器真的过大了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对使用数据库包装器进行数据进行了一些研究. 但是,我读过一些帖子,人们声称您不应该将PDO用于数据库包装程序,因为它已经是一个.

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.

  1. 您可以在一类中处理所有数据操作(原始数据),而不是散布在网站文件中.因此,调试和处理错误要容易得多.
  2. 您可以轻松地使用另一个数据库类来更改您的类.
  3. 您不必重复代码,只需在数据库类中调用代码
  4. 您可以选择扩展类,例如记录日志,统计测试......

示例:

<?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;
    }
}
?>

您可以调用get()并传递查询和一些可选参数,而不是不断重复获取数据的所有步骤. 这比每次打开连接,执行三行代码并关闭它都要有效.

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));

推荐答案

据说,因为大多数创建PDO包装器的尝试确实是无助的,而且比原始PDO会使事情变得更糟.
另外,前提是,基于其编写包装器的前提是错误的.

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.

让我们带你去吧

您可以一类处理所有数据,而不是分散在您的网站文件中.

You handle all your data in one class, not spread across your website files.

非常模糊的陈述,没有特殊含义.当然,您可以在整个应用程序中处理数据,但不使用原始PDO,而是使用您自己的类.

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.

不过是一种妄想.您实际上不能坚持使用两种辅助方法-有时您需要使用原始PDO实例.

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

这是真的.但是没有其他任何API有用.

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.

与每次打开连接,执行3行代码并关闭它相比,效率要高得多.

This is a lot more efficiënt than everytime open a connection, perform 3 lines of code and close it.

这是错误的. 没人每次打开连接,执行3行代码并将其关闭时都会询问您.即使原始PDO连接也只能打开一次.

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.

但是,您的实现非常好.它不会对原始PSO添加太多(实际上,它仅将重复操作仅缩短了一行),但是仍然很明智.所以-我称这种方法相当成功.

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.

一些建议:

  1. 将数据库凭据存储为类属性确实没有意义.他们只需要在构造函数中就不需要了
  2. 为什么不直接在构造函数中创建连接,而无需额外的方法?
  3. 公共函数getOne()将是对您的集合的必不可少的补充.
  4. 您需要公开$ connection-至少要等到PDO和PDOstatement都可以使用所有方法为止

这篇关于pdo包装器真的过大了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆