1. sfPDODatabase.class.php
  2. /** * sfPDODatabase provides connectivity for the PDO database abstraction layer. * * @package symfony * @subpackage database * @author Daniel Swarbrick (daniel@pressure.net.nz) * @author Fabien Potencier * @author Sean Kerr * @author Dustin Whittle * @version SVN: $Id: sfPDODatabase.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. class sfPDODatabase extends sfDatabase
  4. {
  5. /**
  6. * Connects to the database.
  7. *
  8. * @throws <b>sfDatabaseException</b> If a connection could not be created
  9. */
  10. public function connect()
  11. {
  12. if (!$dsn = $this->getParameter('dsn'))
  13. {
  14. // missing required dsn parameter
  15. throw new sfDatabaseException('Database configuration is missing the "dsn" parameter.');
  16. }
  17. try
  18. {
  19. $pdo_class = $this->getParameter('class', 'PDO');
  20. $username = $this->getParameter('username');
  21. $password = $this->getParameter('password');
  22. $persistent = $this->getParameter('persistent');
  23. $options = ($persistent) ? array(PDO::ATTR_PERSISTENT => true) : array();
  24. $this->connection = new $pdo_class($dsn, $username, $password, $options);
  25. }
  26. catch (PDOException $e)
  27. {
  28. throw new sfDatabaseException($e->getMessage());
  29. }
  30. // lets generate exceptions instead of silent failures
  31. if (sfConfig::get('sf_debug'))
  32. {
  33. $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  34. }
  35. else
  36. {
  37. $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
  38. }
  39. // compatability
  40. $compatability = $this->getParameter('compat');
  41. if ($compatability)
  42. {
  43. $this->connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
  44. }
  45. // nulls
  46. $nulls = $this->getParameter('nulls');
  47. if ($nulls)
  48. {
  49. $this->connection->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
  50. }
  51. // auto commit
  52. $autocommit = $this->getParameter('autocommit');
  53. if ($autocommit)
  54. {
  55. $this->connection->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
  56. }
  57. $this->resource = $this->connection;
  58. }
  59. /**
  60. * Execute the shutdown procedure.
  61. *
  62. * @return void
  63. */
  64. public function shutdown()
  65. {
  66. if ($this->connection !== null)
  67. {
  68. @$this->connection = null;
  69. }
  70. }
  71. /**
  72. * Magic method for calling PDO directly via sfPDODatabase
  73. *
  74. * @param string $method
  75. * @param array $arguments
  76. * @return mixed
  77. */
  78. public function __call($method, $arguments)
  79. {
  80. return $this->getConnection()->$method($arguments);
  81. }
  82. }

Debug toolbar