Abstract.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_Validate
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * Class for Database record validation
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @uses Zend_Validate_Abstract
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract
  35. {
  36. /**
  37. * Error constants
  38. */
  39. const ERROR_NO_RECORD_FOUND = 'noRecordFound';
  40. const ERROR_RECORD_FOUND = 'recordFound';
  41. /**
  42. * @var array Message templates
  43. */
  44. protected $_messageTemplates = array(self::ERROR_NO_RECORD_FOUND => 'No record matching %value% was found',
  45. self::ERROR_RECORD_FOUND => 'A record matching %value% was found');
  46. /**
  47. * @var string
  48. */
  49. protected $_schema = null;
  50. /**
  51. * @var string
  52. */
  53. protected $_table = '';
  54. /**
  55. * @var string
  56. */
  57. protected $_field = '';
  58. /**
  59. * @var mixed
  60. */
  61. protected $_exclude = null;
  62. /**
  63. * Database adapter to use. If null isValid() will use Zend_Db::getInstance instead
  64. *
  65. * @var unknown_type
  66. */
  67. protected $_adapter = null;
  68. /**
  69. * Provides basic configuration for use with Zend_Validate_Db Validators
  70. * Setting $exclude allows a single record to be excluded from matching.
  71. * Exclude can either be a String containing a where clause, or an array with `field` and `value` keys
  72. * to define the where clause added to the sql.
  73. * A database adapter may optionally be supplied to avoid using the registered default adapter.
  74. *
  75. * @param string||array $table The database table to validate against, or array with table and schema keys
  76. * @param string $field The field to check for a match
  77. * @param string||array $exclude An optional where clause or field/value pair to exclude from the query
  78. * @param Zend_Db_Adapter_Abstract $adapter An optional database adapter to use.
  79. */
  80. public function __construct($table, $field, $exclude = null, Zend_Db_Adapter_Abstract $adapter = null)
  81. {
  82. if ($adapter !== null) {
  83. $this->_adapter = $adapter;
  84. }
  85. $this->_exclude = $exclude;
  86. $this->_field = (string) $field;
  87. if (is_array($table)) {
  88. $this->_table = (isset($table['table'])) ? $table['table'] : '';
  89. $this->_schema = (isset($table['schema'])) ? $table['schema'] : null;
  90. } else {
  91. $this->_table = (string) $table;
  92. }
  93. }
  94. /**
  95. * Run query and returns matches, or null if no matches are found.
  96. *
  97. * @param String $value
  98. * @return Array when matches are found.
  99. */
  100. protected function _query($value)
  101. {
  102. /**
  103. * Check for an adapter being defined. if not, fetch the default adapter.
  104. */
  105. if ($this->_adapter === null) {
  106. $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
  107. if (null === $this->_adapter) {
  108. require_once 'Zend/Validate/Exception.php';
  109. throw new Zend_Validate_Exception('No database adapter present');
  110. }
  111. }
  112. /**
  113. * Build select object
  114. */
  115. $select = new Zend_Db_Select($this->_adapter);
  116. $select->from($this->_table, array($this->_field), $this->_schema)
  117. ->where($this->_adapter->quoteIdentifier($this->_field).' = ?', $value);
  118. if ($this->_exclude !== null) {
  119. if (is_array($this->_exclude)) {
  120. $select->where($this->_adapter->quoteIdentifier($this->_exclude['field']).' != ?', $this->_exclude['value']);
  121. } else {
  122. $select->where($this->_exclude);
  123. }
  124. }
  125. $select->limit(1);
  126. /**
  127. * Run query
  128. */
  129. $result = $this->_adapter->fetchRow($select, array(), Zend_Db::FETCH_ASSOC);
  130. return $result;
  131. }
  132. }