在PHP中,如何设置默认的PDO Fetch Class?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了在PHP中,如何设置默认的PDO Fetch Class?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
使用 PDO::setAttribute,如何在将PDO :: ATTR_DEFAULT_FETCH_MODE设置为PDO :: FETCH_CLASS时提供类名.

这是我正在使用的代码..我想设置它所以我的所有行都作为DB_Row的实例返回:

class DB_Row extends arrayobject {}

$db = new PDO('MysqL:dbname=example;host=localhost','user','pass');
$db->setattribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_CLASS);

$stmt = $db->query("SELECT * From `table` WHERE `id` = 1;");

$row = $stmt->fetch(); // I want a DB_Row by default!

上面的代码导致PDOException,因为未分配DB_Row类名.

Fatal error: Uncaught exception 'PDOException' wITh message 'sqlstatE[HY000]: General    error: No fetch class sPEcified

我该怎么做?

提前致谢..

解决方案:我使用了fireeyedboy的答案.它对我的情况起了最好的作用,因为我已经将PDOStatement扩展用于记录目的……

class DB extends PDO {
    public function __construct($host = null,$user = null,$pass = null,$db = null) {
        try {
            parent::__construct('MysqL:dbname=' . $name .';host=' . $host,$user,$pass);
            $this->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_CLASS);
            $this->setAttribute(PDO::ATTR_STATEMENT_CLASS,array('DB_Query',array('DB_Row')));
        } catch (PDOException $e) {
            die('Database Error');
        }
    }
}

class DB_Query extends PDOStatement {
    PRivate $class;
    protected function __construct ($class = 'DB_Row') {
        $this->class = $class;
        $this->setFetchMode(PDO::FETCH_CLASS,$this->class);
    }
}

class DB_Row extends ArrayObject {
    public function __set($name,$val) {
        $this[$name] = $val;
    }
    public function __get($name) {
        return $this[$name];
    }
}
另一种破解方法是扩展PDOStatement,覆盖其获取方法,并让您的PDO实例将其用作认语句类.

作为一个例子,我只是演示覆盖fetch()1并留下fetchAll()以及你有什么打算,如果你想走这条路:

class Db_Row
{
}

class PDOStatementWithClass
    extends PDOStatement
{
    private $fetch_class;

    // PHP complained when I tried to make this public
    protected function __construct( $fetch_class = 'StdClass' )
    {
        // internally set the fetch class for later use
        $this->fetch_class = $fetch_class;
    }

    // let $fetch_style default to PDO::FETCH_CLASS in stead of PDO::FETCH_BOTH
    public function fetch( $fetch_style = PDO::FETCH_CLASS,$cursor_orientation = PDO::FETCH_ORI_NEXT,$cursor_offset = 0 )
    {
        // make sure we're really dealing with the correct fetch style
        if( $fetch_style == PDO::FETCH_CLASS )
        {
            // then automatically set the fetch mode of this statement
            parent::setFetchMode( $fetch_style,$this->fetch_class );
        }

        // go ahead and fetch,we should be good Now
        return parent::fetch( $fetch_style,$cursor_orientation,$cursor_offset );
    }
}

$db = new PDO( /* etc... */ );
// set default fetch mode to FETCH_CLASS
$db->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_CLASS );
// override what statement class to use,and provide constructor arguments (found out by trial and error)
$db->setAttribute( PDO::ATTR_STATEMENT_CLASS,array( 'PDOStatementWithClass',array( 'Db_Row' ) ) );

这样做的另一个好处是,您只需在应用程序中定义一次PDO :: FETCH_CLASS,而不是在每个查询中定义.

1)我很惊讶PHP并没有抱怨重写方法的签名.

脚本宝典总结

以上是脚本宝典为你收集整理的在PHP中,如何设置默认的PDO Fetch Class?全部内容,希望文章能够帮你解决在PHP中,如何设置默认的PDO Fetch Class?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。