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

前端之家收集整理的这篇文章主要介绍了在PHP中,如何设置默认的PDO Fetch Class?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用 PDO::setAttribute,如何在将PDO :: ATTR_DEFAULT_FETCH_MODE设置为PDO :: FETCH_CLASS时提供类名.

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

  1. class DB_Row extends ArrayObject {}
  2.  
  3. $db = new PDO('MysqL:dbname=example;host=localhost','user','pass');
  4. $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_CLASS);
  5.  
  6. $stmt = $db->query("SELECT * FROM `table` WHERE `id` = 1;");
  7.  
  8. $row = $stmt->fetch(); // I want a DB_Row by default!

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

  1. Fatal error: Uncaught exception 'PDOException' with message 'sqlSTATE[HY000]: General error: No fetch class specified

我该怎么做?

提前致谢..

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

  1. class DB extends PDO {
  2. public function __construct($host = null,$user = null,$pass = null,$db = null) {
  3. try {
  4. parent::__construct('MysqL:dbname=' . $name .';host=' . $host,$user,$pass);
  5. $this->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  6. $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_CLASS);
  7. $this->setAttribute(PDO::ATTR_STATEMENT_CLASS,array('DB_Query',array('DB_Row')));
  8. } catch (PDOException $e) {
  9. die('Database Error');
  10. }
  11. }
  12. }
  13.  
  14. class DB_Query extends PDOStatement {
  15. private $class;
  16. protected function __construct ($class = 'DB_Row') {
  17. $this->class = $class;
  18. $this->setFetchMode(PDO::FETCH_CLASS,$this->class);
  19. }
  20. }
  21.  
  22. class DB_Row extends ArrayObject {
  23. public function __set($name,$val) {
  24. $this[$name] = $val;
  25. }
  26. public function __get($name) {
  27. return $this[$name];
  28. }
  29. }
另一种破解方法是扩展PDOStatement,覆盖其获取方法,并让您的PDO实例将其用作默认语句类.

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

  1. class Db_Row
  2. {
  3. }
  4.  
  5. class PDOStatementWithClass
  6. extends PDOStatement
  7. {
  8. private $fetch_class;
  9.  
  10. // PHP complained when I tried to make this public
  11. protected function __construct( $fetch_class = 'StdClass' )
  12. {
  13. // internally set the fetch class for later use
  14. $this->fetch_class = $fetch_class;
  15. }
  16.  
  17. // let $fetch_style default to PDO::FETCH_CLASS in stead of PDO::FETCH_BOTH
  18. public function fetch( $fetch_style = PDO::FETCH_CLASS,$cursor_orientation = PDO::FETCH_ORI_NEXT,$cursor_offset = 0 )
  19. {
  20. // make sure we're really dealing with the correct fetch style
  21. if( $fetch_style == PDO::FETCH_CLASS )
  22. {
  23. // then automatically set the fetch mode of this statement
  24. parent::setFetchMode( $fetch_style,$this->fetch_class );
  25. }
  26.  
  27. // go ahead and fetch,we should be good now
  28. return parent::fetch( $fetch_style,$cursor_orientation,$cursor_offset );
  29. }
  30. }
  31.  
  32. $db = new PDO( /* etc... */ );
  33. // set default fetch mode to FETCH_CLASS
  34. $db->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_CLASS );
  35. // override what statement class to use,and provide constructor arguments (found out by trial and error)
  36. $db->setAttribute( PDO::ATTR_STATEMENT_CLASS,array( 'PDOStatementWithClass',array( 'Db_Row' ) ) );

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

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

猜你在找的PHP相关文章