PageRenderTime 43ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Validate/Db/Abstract.php

https://github.com/yichaowang/rbczf
PHP | 352 lines | 160 code | 33 blank | 159 comment | 24 complexity | 39c21ebeda1d1e106e2c26967a7adef8 MD5 | raw file
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Abstract.php 23356 2010-11-18 15:59:10Z ralph $
  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-2010 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(
  45. self::ERROR_NO_RECORD_FOUND => "No record matching '%value%' was found",
  46. //self::ERROR_RECORD_FOUND => "A record matching '%value%' was found"
  47. self::ERROR_RECORD_FOUND => "'%value%' is already registered with us. Please contact administrator if you believe there is a error."
  48. );
  49. /**
  50. * @var string
  51. */
  52. protected $_schema = null;
  53. /**
  54. * @var string
  55. */
  56. protected $_table = '';
  57. /**
  58. * @var string
  59. */
  60. protected $_field = '';
  61. /**
  62. * @var mixed
  63. */
  64. protected $_exclude = null;
  65. /**
  66. * Database adapter to use. If null isValid() will use Zend_Db::getInstance instead
  67. *
  68. * @var unknown_type
  69. */
  70. protected $_adapter = null;
  71. /**
  72. * Select object to use. can be set, or will be auto-generated
  73. * @var Zend_Db_Select
  74. */
  75. protected $_select;
  76. /**
  77. * Provides basic configuration for use with Zend_Validate_Db Validators
  78. * Setting $exclude allows a single record to be excluded from matching.
  79. * Exclude can either be a String containing a where clause, or an array with `field` and `value` keys
  80. * to define the where clause added to the sql.
  81. * A database adapter may optionally be supplied to avoid using the registered default adapter.
  82. *
  83. * The following option keys are supported:
  84. * 'table' => The database table to validate against
  85. * 'schema' => The schema keys
  86. * 'field' => The field to check for a match
  87. * 'exclude' => An optional where clause or field/value pair to exclude from the query
  88. * 'adapter' => An optional database adapter to use
  89. *
  90. * @param array|Zend_Config $options Options to use for this validator
  91. */
  92. public function __construct($options)
  93. {
  94. if ($options instanceof Zend_Db_Select) {
  95. $this->setSelect($options);
  96. return;
  97. }
  98. if ($options instanceof Zend_Config) {
  99. $options = $options->toArray();
  100. } else if (func_num_args() > 1) {
  101. $options = func_get_args();
  102. $temp['table'] = array_shift($options);
  103. $temp['field'] = array_shift($options);
  104. if (!empty($options)) {
  105. $temp['exclude'] = array_shift($options);
  106. }
  107. if (!empty($options)) {
  108. $temp['adapter'] = array_shift($options);
  109. }
  110. $options = $temp;
  111. }
  112. if (!array_key_exists('table', $options) && !array_key_exists('schema', $options)) {
  113. require_once 'Zend/Validate/Exception.php';
  114. throw new Zend_Validate_Exception('Table or Schema option missing!');
  115. }
  116. if (!array_key_exists('field', $options)) {
  117. require_once 'Zend/Validate/Exception.php';
  118. throw new Zend_Validate_Exception('Field option missing!');
  119. }
  120. if (array_key_exists('adapter', $options)) {
  121. $this->setAdapter($options['adapter']);
  122. }
  123. if (array_key_exists('exclude', $options)) {
  124. $this->setExclude($options['exclude']);
  125. }
  126. $this->setField($options['field']);
  127. if (array_key_exists('table', $options)) {
  128. $this->setTable($options['table']);
  129. }
  130. if (array_key_exists('schema', $options)) {
  131. $this->setSchema($options['schema']);
  132. }
  133. }
  134. /**
  135. * Returns the set adapter
  136. *
  137. * @return Zend_Db_Adapter
  138. */
  139. public function getAdapter()
  140. {
  141. /**
  142. * Check for an adapter being defined. if not, fetch the default adapter.
  143. */
  144. if ($this->_adapter === null) {
  145. $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
  146. if (null === $this->_adapter) {
  147. require_once 'Zend/Validate/Exception.php';
  148. throw new Zend_Validate_Exception('No database adapter present');
  149. }
  150. }
  151. return $this->_adapter;
  152. }
  153. /**
  154. * Sets a new database adapter
  155. *
  156. * @param Zend_Db_Adapter_Abstract $adapter
  157. * @return Zend_Validate_Db_Abstract
  158. */
  159. public function setAdapter($adapter)
  160. {
  161. if (!($adapter instanceof Zend_Db_Adapter_Abstract)) {
  162. require_once 'Zend/Validate/Exception.php';
  163. throw new Zend_Validate_Exception('Adapter option must be a database adapter!');
  164. }
  165. $this->_adapter = $adapter;
  166. return $this;
  167. }
  168. /**
  169. * Returns the set exclude clause
  170. *
  171. * @return string|array
  172. */
  173. public function getExclude()
  174. {
  175. return $this->_exclude;
  176. }
  177. /**
  178. * Sets a new exclude clause
  179. *
  180. * @param string|array $exclude
  181. * @return Zend_Validate_Db_Abstract
  182. */
  183. public function setExclude($exclude)
  184. {
  185. $this->_exclude = $exclude;
  186. return $this;
  187. }
  188. /**
  189. * Returns the set field
  190. *
  191. * @return string|array
  192. */
  193. public function getField()
  194. {
  195. return $this->_field;
  196. }
  197. /**
  198. * Sets a new field
  199. *
  200. * @param string $field
  201. * @return Zend_Validate_Db_Abstract
  202. */
  203. public function setField($field)
  204. {
  205. $this->_field = (string) $field;
  206. return $this;
  207. }
  208. /**
  209. * Returns the set table
  210. *
  211. * @return string
  212. */
  213. public function getTable()
  214. {
  215. return $this->_table;
  216. }
  217. /**
  218. * Sets a new table
  219. *
  220. * @param string $table
  221. * @return Zend_Validate_Db_Abstract
  222. */
  223. public function setTable($table)
  224. {
  225. $this->_table = (string) $table;
  226. return $this;
  227. }
  228. /**
  229. * Returns the set schema
  230. *
  231. * @return string
  232. */
  233. public function getSchema()
  234. {
  235. return $this->_schema;
  236. }
  237. /**
  238. * Sets a new schema
  239. *
  240. * @param string $schema
  241. * @return Zend_Validate_Db_Abstract
  242. */
  243. public function setSchema($schema)
  244. {
  245. $this->_schema = $schema;
  246. return $this;
  247. }
  248. /**
  249. * Sets the select object to be used by the validator
  250. *
  251. * @param Zend_Db_Select $select
  252. * @return Zend_Validate_Db_Abstract
  253. */
  254. public function setSelect($select)
  255. {
  256. if (!$select instanceof Zend_Db_Select) {
  257. throw new Zend_Validate_Exception('Select option must be a valid ' .
  258. 'Zend_Db_Select object');
  259. }
  260. $this->_select = $select;
  261. return $this;
  262. }
  263. /**
  264. * Gets the select object to be used by the validator.
  265. * If no select object was supplied to the constructor,
  266. * then it will auto-generate one from the given table,
  267. * schema, field, and adapter options.
  268. *
  269. * @return Zend_Db_Select The Select object which will be used
  270. */
  271. public function getSelect()
  272. {
  273. if (null === $this->_select) {
  274. $db = $this->getAdapter();
  275. /**
  276. * Build select object
  277. */
  278. $select = new Zend_Db_Select($db);
  279. $select->from($this->_table, array($this->_field), $this->_schema);
  280. if ($db->supportsParameters('named')) {
  281. $select->where($db->quoteIdentifier($this->_field, true).' = :value'); // named
  282. } else {
  283. $select->where($db->quoteIdentifier($this->_field, true).' = ?'); // positional
  284. }
  285. if ($this->_exclude !== null) {
  286. if (is_array($this->_exclude)) {
  287. $select->where(
  288. $db->quoteIdentifier($this->_exclude['field'], true) .
  289. ' != ?', $this->_exclude['value']
  290. );
  291. } else {
  292. $select->where($this->_exclude);
  293. }
  294. }
  295. $select->limit(1);
  296. $this->_select = $select;
  297. }
  298. return $this->_select;
  299. }
  300. /**
  301. * Run query and returns matches, or null if no matches are found.
  302. *
  303. * @param String $value
  304. * @return Array when matches are found.
  305. */
  306. protected function _query($value)
  307. {
  308. $select = $this->getSelect();
  309. /**
  310. * Run query
  311. */
  312. $result = $select->getAdapter()->fetchRow(
  313. $select,
  314. array('value' => $value), // this should work whether db supports positional or named params
  315. Zend_Db::FETCH_ASSOC
  316. );
  317. return $result;
  318. }
  319. }