2
0

Db.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Application
  17. * @subpackage Resource
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Resource for creating database adapter
  24. *
  25. * @uses Zend_Application_Resource_ResourceAbstract
  26. * @category Zend
  27. * @package Zend_Application
  28. * @subpackage Resource
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Application_Resource_Db extends Zend_Application_Resource_ResourceAbstract
  33. {
  34. /**
  35. * Adapter to use
  36. *
  37. * @var string
  38. */
  39. protected $_adapter = null;
  40. /**
  41. * @var Zend_Db_Adapter_Interface
  42. */
  43. protected $_db;
  44. /**
  45. * Parameters to use
  46. *
  47. * @var array
  48. */
  49. protected $_params = array();
  50. /**
  51. * Wether to register the created adapter as default table adapter
  52. *
  53. * @var boolean
  54. */
  55. protected $_isDefaultTableAdapter = true;
  56. /**
  57. * Set the adapter
  58. *
  59. * @param $adapter string
  60. * @return Zend_Application_Resource_Db
  61. */
  62. public function setAdapter($adapter)
  63. {
  64. $this->_adapter = $adapter;
  65. return $this;
  66. }
  67. /**
  68. * Adapter type to use
  69. *
  70. * @return string
  71. */
  72. public function getAdapter()
  73. {
  74. return $this->_adapter;
  75. }
  76. /**
  77. * Set the adapter params
  78. *
  79. * @param $adapter string
  80. * @return Zend_Application_Resource_Db
  81. */
  82. public function setParams(array $params)
  83. {
  84. $this->_params = $params;
  85. return $this;
  86. }
  87. /**
  88. * Adapter parameters
  89. *
  90. * @return array
  91. */
  92. public function getParams()
  93. {
  94. return $this->_params;
  95. }
  96. /**
  97. * Set whether to use this as default table adapter
  98. *
  99. * @param boolean $defaultTableAdapter
  100. * @return Zend_Application_Resource_Db
  101. */
  102. public function setIsDefaultTableAdapter($isDefaultTableAdapter)
  103. {
  104. $this->_isDefaultTableAdapter = $isDefaultTableAdapter;
  105. return $this;
  106. }
  107. /**
  108. * Is this adapter the default table adapter?
  109. *
  110. * @return void
  111. */
  112. public function isDefaultTableAdapter()
  113. {
  114. return $this->_isDefaultTableAdapter;
  115. }
  116. /**
  117. * Retrieve initialized DB connection
  118. *
  119. * @return null|Zend_Db_Adapter_Interface
  120. */
  121. public function getDbAdapter()
  122. {
  123. if ((null === $this->_db)
  124. && (null !== ($adapter = $this->getAdapter()))
  125. ) {
  126. $this->_db = Zend_Db::factory($adapter, $this->getParams());
  127. }
  128. return $this->_db;
  129. }
  130. /**
  131. * Defined by Zend_Application_Resource_Resource
  132. *
  133. * @return Zend_Db_Adapter_Abstract|null
  134. */
  135. public function init()
  136. {
  137. if (null !== ($db = $this->getDbAdapter())) {
  138. if ($this->isDefaultTableAdapter()) {
  139. Zend_Db_Table::setDefaultAdapter($db);
  140. }
  141. return $db;
  142. }
  143. }
  144. }