Abstract.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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-2008 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-2008 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 $_table = '';
  50. /**
  51. * @var string
  52. */
  53. protected $_field = '';
  54. /**
  55. * @var mixed
  56. */
  57. protected $_exclude = null;
  58. /**
  59. * Database adapter to use. If null isValid() will use Zend_Db::getInstance instead
  60. *
  61. * @var unknown_type
  62. */
  63. protected $_adapter = null;
  64. /**
  65. * Provides basic configuration for use with Zend_Validate_Db Validators
  66. * Setting $exclude allows a single record to be excluded from matching.
  67. * Exclude can either be a String containing a where clause, or an array with `field` and `value` keys
  68. * to define the where clause added to the sql.
  69. * A database adapter may optionally be supplied to avoid using the registered default adapter.
  70. *
  71. * @param string $table The database table to validate against
  72. * @param string $field The field to check for a match
  73. * @param string||array $exclude An optional where clause or field/value pair to exclude from the query
  74. * @param Zend_Db_Adapter_Abstract $adapter An optional database adapter to use.
  75. */
  76. public function __construct($table, $field, $exclude = null, Zend_Db_Adapter_Abstract $adapter = null)
  77. {
  78. if ($adapter !== null) {
  79. $this->_adapter = $adapter;
  80. }
  81. $this->_exclude = $exclude;
  82. $this->_table = (string) $table;
  83. $this->_field = (string) $field;
  84. }
  85. /**
  86. * Run query and returns matches, or null if no matches are found.
  87. *
  88. * @param String $value
  89. * @return Array when matches are found.
  90. */
  91. protected function _query($value)
  92. {
  93. /**
  94. * Check for an adapter being defined. if not, fetch the default adapter.
  95. */
  96. if($this->_adapter === null) {
  97. $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
  98. }
  99. /**
  100. * Build select object
  101. */
  102. $select = new Zend_Db_Select($this->_adapter);
  103. $select->from($this->_table, array($this->_field))
  104. ->where($this->_adapter->quoteIdentifier($this->_field).' = ?', $value);
  105. if ($this->_exclude !== null) {
  106. if (is_array($this->_exclude)) {
  107. $select->where($this->_adapter->quoteIdentifier($this->_exclude['field']).' != ?', $this->_exclude['value']);
  108. } else {
  109. $select->where($this->_exclude);
  110. }
  111. }
  112. $select->limit(1);
  113. /**
  114. * Run query
  115. */
  116. $result = $this->_adapter->fetchRow($select, array(), Zend_Db::FETCH_ASSOC);
  117. return $result;
  118. }
  119. }