【问题标题】:Extend PDO prepare method to replace query prefix扩展 PDO 准备方法以替换查询前缀
【发布时间】:2015-11-02 18:48:37
【问题描述】:

我在下面有一个代码并尝试更改此行:

$sth    = $db->prepare(Utils::prepareSQLPrefix($sql, Config::get('DB_PREFIX')));

在我的 DatabaseFactory 类中扩展某种 $db->prepare 函数(替换查询前缀)。

$db         = DatabaseFactory::getFactory()->getConnection();
$sql        = ' SELECT 
                    `id`, `manifest`
                FROM 
                    `#__extensions`
                WHERE 
                    `name` = :name
                LIMIT 1';

        $sth    = $db->prepare(Utils::prepareSQLPrefix($sql, Config::get('DB_PREFIX')));
        $sth->execute(array('name' =>$extension->component_name));
        $result = $sth->fetch();

我的 DatabaseFactory 类:

class DatabaseFactory {
    private static $factory;
    private $database;

    public static function getFactory()
    {
        if (!self::$factory) {
            self::$factory = new DatabaseFactory();
        }
        return self::$factory;
    }

    public function getConnection() {
        if (!$this->database) {
            $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
            $this->database = new PDO(
                    Config::get('DB_TYPE') . ':host=' . Config::get('DB_HOST') . ';dbname=' .
                    Config::get('DB_NAME') . ';port=' . Config::get('DB_PORT') . ';charset=' . Config::get('DB_CHARSET'),
                    Config::get('DB_USER'), Config::get('DB_PASS'), $options
            );

        }

        return $this->database;
    }
}

我的 Utils 类:

class Utils { 
    public static function prepareSQLPrefix($sql, $prefix) {
        return str_replace('#__', $prefix, $sql);   
    }
}

我该怎么做?谢谢!

【问题讨论】:

  • 您的代码似乎有效。你有什么问题或问题?
  • 我想将 prepareSQLPrefix 移至 DatabaseFactory 类
  • 您应该在 DatabaseFactory 类上使用 _construct,然后在 Utils 上使用 parent::construct。或使用 protected 并将 DatabaseFactory 扩展到子类
  • 这里$sth->execute(array('name' =>$extension->component_name));你应该使用:name而不是name

标签: php mysql sql database pdo


【解决方案1】:

我猜你应该创建一个扩展 PDO 的新类并重新定义 prepare 函数:

class myPDO extends PDO {

    public function prepare ($sql, $options = NULL) {
        $sql = Utils::prepareSQLPrefix($sql, Config::get('DB_PREFIX'));
        return parent::prepare($sql, $options);
    }

}

然后你把你的工厂getConnection改成:

 public function getConnection() {
    if (!$this->database) {
        $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
        $this->database = new myPDO(
                Config::get('DB_TYPE') . ':host=' . Config::get('DB_HOST') . ';dbname=' .
                Config::get('DB_NAME') . ';port=' . Config::get('DB_PORT') . ';charset=' . Config::get('DB_CHARSET'),
                Config::get('DB_USER'), Config::get('DB_PASS'), $options
        );

    }

    return $this->database;
}

然后代替:

$sth    = $db->prepare(Utils::prepareSQLPrefix($sql, Config::get('DB_PREFIX')));

你可以:

$sth    = $db->prepare($sql);

得到相同的预期结果

【讨论】:

  • 谢谢!我已经更新了我的代码,但得到一个错误:严格标准:DB::prepare() 的声明应该与 ...\core\DB.php 中的 PDO::prepare($statement, $options = NULL) 兼容52
  • 因为你是按你的方式做的,而不是我的。做我说的,但不要做你认为你理解的。类myPDO 应该与一个重新定义的方法分开。但你的做法不同——为什么?你这样做 $this->database = new DB( 所以这似乎是 DB 类中的递归调用你为什么这样做?
  • @user889349 顺便说一句,我已经阅读了您发布的错误,因此将 , $options = NULL 添加到 prepare 函数重新定义
  • 对不起,我不明白。你能用全班更新你的答案吗?我该如何使用它的例子?谢谢。
  • 如果您的 PHP 版本 >= 5.10,您应该将 public function prepare ($sql, $options = NULL) 更改为 public function prepare ($sql, $options = array())
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 2015-12-08
  • 1970-01-01
  • 2014-05-23
  • 2013-09-07
  • 2015-10-21
  • 1970-01-01
相关资源
最近更新 更多