00001 <?php 00002 00026 require_once 'Zend/Validate/Abstract.php'; 00027 00037 abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract 00038 { 00042 const ERROR_NO_RECORD_FOUND = 'noRecordFound'; 00043 const ERROR_RECORD_FOUND = 'recordFound'; 00044 00048 protected $_messageTemplates = array(self::ERROR_NO_RECORD_FOUND => 'No record matching %value% was found', 00049 self::ERROR_RECORD_FOUND => 'A record matching %value% was found'); 00050 00054 protected $_schema = null; 00055 00059 protected $_table = ''; 00060 00064 protected $_field = ''; 00065 00069 protected $_exclude = null; 00070 00076 protected $_adapter = null; 00077 00090 public function __construct($table, $field, $exclude = null, Zend_Db_Adapter_Abstract $adapter = null) 00091 { 00092 if ($adapter !== null) { 00093 $this->_adapter = $adapter; 00094 } 00095 $this->_exclude = $exclude; 00096 $this->_field = (string) $field; 00097 00098 if (is_array($table)) { 00099 $this->_table = (isset($table['table'])) ? $table['table'] : ''; 00100 $this->_schema = (isset($table['schema'])) ? $table['schema'] : null; 00101 } else { 00102 $this->_table = (string) $table; 00103 } 00104 00105 } 00106 00113 protected function _query($value) 00114 { 00118 if ($this->_adapter === null) { 00119 $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter(); 00120 if (null === $this->_adapter) { 00121 require_once 'Zend/Validate/Exception.php'; 00122 throw new Zend_Validate_Exception('No database adapter present'); 00123 } 00124 } 00125 00129 $select = new Zend_Db_Select($this->_adapter); 00130 $select->from($this->_table, array($this->_field), $this->_schema) 00131 ->where($this->_adapter->quoteIdentifier($this->_field).' = ?', $value); 00132 if ($this->_exclude !== null) { 00133 if (is_array($this->_exclude)) { 00134 $select->where($this->_adapter->quoteIdentifier($this->_exclude['field']).' != ?', $this->_exclude['value']); 00135 } else { 00136 $select->where($this->_exclude); 00137 } 00138 } 00139 $select->limit(1); 00140 00144 $result = $this->_adapter->fetchRow($select, array(), Zend_Db::FETCH_ASSOC); 00145 00146 return $result; 00147 } 00148 }