扩展 PDO 语句类

发布于 2024-09-13 04:00:49 字数 91 浏览 5 评论 0原文

是否可以扩展 PHP PDO 语句类以向其添加自定义方法?这与扩展 PDO 基类不同。如果是这样,由于仅在通过 PDO 类运行查询时才返回语句类,因此将如何执行此操作?

Is it possible to extend PHP PDO statement class to add custom methods to it? This would be different from extending the base PDO class. If so, how would one go about doing it since the statement class is only returned when running queries through the PDO class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乱了心跳 2024-09-20 04:00:49

您可以使用 PDO::setAttribute()

PDO::ATTR_STATEMENT_CLASS:设置从 PDOStatement 派生的用户提供的语句类。不能与持久 PDO 实例一起使用。需要数组(字符串类名,数组(混合构造函数_参数))。

例子:

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Custom::class]);

You can set the class with PDO::setAttribute():

PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).

Example:

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Custom::class]);
凉墨 2024-09-20 04:00:49

这是由 PDO 下的 PHP 手册中的用户回答的:

class Database extends PDO {
    function __construct($dsn, $username="", $password="", $driver_options=array()) {
        parent::__construct($dsn,$username,$password, $driver_options);
        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('DBStatement', array($this)));
    }
}
class DBStatement extends PDOStatement {
    public $dbh;
    protected function __construct($dbh) {
        $this->dbh = $dbh;
    }
}

您可以通过在此页面搜索“smileaf”找到他的原始答案:https://php.net/manual/en/book.pdo.php

This is answered by a user in the PHP Manual under PDO:

class Database extends PDO {
    function __construct($dsn, $username="", $password="", $driver_options=array()) {
        parent::__construct($dsn,$username,$password, $driver_options);
        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('DBStatement', array($this)));
    }
}
class DBStatement extends PDOStatement {
    public $dbh;
    protected function __construct($dbh) {
        $this->dbh = $dbh;
    }
}

You can find his original answer by searching: 'smileaf' at this page: https://php.net/manual/en/book.pdo.php

沐歌 2024-09-20 04:00:49

这是我的代码,用于将选择查询的结果作为插入语句保存到文本文件中。
我首先扩展 PDOStatement 类以添加自定义方法 saveResultAsInsertStatement

<?php
class MyPDOStatement extends PDOStatement {
    protected $pdo;

    protected function __construct($pdo) {
        $this->pdo = $pdo;
    }
    public function saveResultAsInsertStatement($filename) {
        $result = '';
        $columnData = $this->fetchAll(PDO::FETCH_ASSOC);
        if ($columnData != null) {
            $fieldCount = count($columnData[0]);
            $rowsCount = count($columnData);
            $columnsName = array_keys($columnData[0]);
            $result = "INSERT INTO %s ( \n";
            $result .= join(",\n", $columnsName);
            $result .= ") VALUES\n";
            $r = 0;
            foreach ($columnData as $row) {
                $result .= "(";
                $c = 0;
                foreach ($row as $key => $field) {
                    $result .= $this->pdo->quote($field);
                    $result .= ( ++$c < $fieldCount) ? ', ' : '';
                }
                $result .= ")";
                $result .= ( ++$r < $rowsCount) ? ',' : '';
                $result .= "\n";
            }
        }

        $f = fopen($filename, "w");
        fwrite($f, $result);
        fclose($f);
    }

}
?>

然后我扩展 PDO 类以设置属性 PDO::ATTR_STATMENT_CLASS

<?php
class MyPDO extends PDO {
    public function __construct(... PDO constructor parameters here ... ) {
        parent::__construct( ... PDO construct parameters here ...);
        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement', array($this)));
    }

}
?>

那么,我可以编写:

<?php
$conn = new MyPDO(... PDO constructor parameters here ...);

$sql = ... your select statement here...

$conn->query($sql)->saveResultAsInsertStatement(... name of the file here ...);


?>

This is my code to save the result of a select query into a text file as insert statement.
I first extend the PDOStatement class to add the custom method saveResultAsInsertStatement:

<?php
class MyPDOStatement extends PDOStatement {
    protected $pdo;

    protected function __construct($pdo) {
        $this->pdo = $pdo;
    }
    public function saveResultAsInsertStatement($filename) {
        $result = '';
        $columnData = $this->fetchAll(PDO::FETCH_ASSOC);
        if ($columnData != null) {
            $fieldCount = count($columnData[0]);
            $rowsCount = count($columnData);
            $columnsName = array_keys($columnData[0]);
            $result = "INSERT INTO %s ( \n";
            $result .= join(",\n", $columnsName);
            $result .= ") VALUES\n";
            $r = 0;
            foreach ($columnData as $row) {
                $result .= "(";
                $c = 0;
                foreach ($row as $key => $field) {
                    $result .= $this->pdo->quote($field);
                    $result .= ( ++$c < $fieldCount) ? ', ' : '';
                }
                $result .= ")";
                $result .= ( ++$r < $rowsCount) ? ',' : '';
                $result .= "\n";
            }
        }

        $f = fopen($filename, "w");
        fwrite($f, $result);
        fclose($f);
    }

}
?>

Then I extend the PDO class to set the attribute PDO::ATTR_STATEMENT_CLASS

<?php
class MyPDO extends PDO {
    public function __construct(... PDO constructor parameters here ... ) {
        parent::__construct( ... PDO construct parameters here ...);
        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement', array($this)));
    }

}
?>

So then, I can write:

<?php
$conn = new MyPDO(... PDO constructor parameters here ...);

$sql = ... your select statement here...

$conn->query($sql)->saveResultAsInsertStatement(... name of the file here ...);


?>
转角预定愛 2024-09-20 04:00:49

如果您为类使用命名空间,则需要在类字符串中添加反斜杠。

没有命名空间:

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Custom', array($pdo)));

有命名空间:

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Namespace\Custom', array($pdo)));

If you are using namespaces for your classes you need to add a backslash to your class string.

Without namespace:

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Custom', array($pdo)));

With namespace:

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Namespace\Custom', array($pdo)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文