在PHP中,如何设置默认PDO Fetch类?
使用,在将在PHP中,如何设置默认PDO Fetch类?,php,pdo,Php,Pdo,使用,在将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_FET
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!
由于未分配DB_行类名,上述代码导致出现PDOException
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: No fetch class specified
我该怎么办
提前谢谢
解决方案:我使用了fireeyedboy的答案。它对我的情况最有效,因为我已经扩展了PDO语句以用于日志记录目的
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];
}
}
我认为它应该返回一个stdclass实例,因此这将是一个bug,但必须在代码中查找它以进行验证。如果到那时还没有被接受的答案,我会的 工作原理是使用PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE,然后提供一个类名作为第一列。虽然这是一个黑客:
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE);
$stmt = $db->query("SELECT 'classname', * FROM `table` WHERE `id` = 1;");
编辑:正如这里承诺的代码。此处提供了相关代码,因此只有在使用fetch_类类型时才可以设置for using fetch()。另一种方法是使用PDOStatement::fetchObject()另一种方法是扩展
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_类
,而不是在每个查询中
1) 我很惊讶PHP没有抱怨重写方法的签名。谢谢!这就是我开始使用的方法。主要原因是我不想在所有查询中定义类名。我没有重写fetch,而是将它添加到构造中:$this->setFetchMode(PDO::fetch_CLASS,$this->CLASS);谢谢@johanne!这个方法确实工作得很好,尽管更新所有使用的查询对我来说很麻烦。PDO最好允许在db结构之外定义类。特别是,在大多数情况下,我计划总是使用同一个类。嗨,请考虑在答案上加上一些解释,以及代码片段。这样,OP可以更好地理解。
public function fetchAll($fetch_style = \PDO::FETCH_CLASS, $fetch_argument = null, $ctor_args = null) {
if ($fetch_style == \PDO::FETCH_CLASS){
parent::setFetchMode( $fetch_style, 'App\Core\DBFetch' );
}
$fetchedData = call_user_func_array(array('parent', __FUNCTION__), func_get_args());
return $fetchedData;
}