Explorar el Código

Issue: ZF-9060, ZF-8442, ZF-8423: Added Ability to pass a select object to Db Validators

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22958 44c647ce-9c0f-0410-b52a-842ac1e357ba
bittarman hace 15 años
padre
commit
6e36214f35
Se han modificado 1 ficheros con 76 adiciones y 27 borrados
  1. 76 27
      library/Zend/Validate/Db/Abstract.php

+ 76 - 27
library/Zend/Validate/Db/Abstract.php

@@ -77,6 +77,12 @@ abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract
     protected $_adapter = null;
 
     /**
+     * Select object to use. can be set, or will be auto-generated
+     * @var Zend_Db_Select
+     */
+    protected $_select;
+
+    /**
      * Provides basic configuration for use with Zend_Validate_Db Validators
      * Setting $exclude allows a single record to be excluded from matching.
      * Exclude can either be a String containing a where clause, or an array with `field` and `value` keys
@@ -94,6 +100,10 @@ abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract
      */
     public function __construct($options)
     {
+        if ($options instanceof Zend_Db_Select) {
+            $this->setSelect($options);
+            return;
+        }
         if ($options instanceof Zend_Config) {
             $options = $options->toArray();
         } else if (func_num_args() > 1) {
@@ -146,6 +156,16 @@ abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract
      */
     public function getAdapter()
     {
+        /**
+         * Check for an adapter being defined. if not, fetch the default adapter.
+         */
+        if ($this->_adapter === null) {
+            $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
+            if (null === $this->_adapter) {
+                require_once 'Zend/Validate/Exception.php';
+                throw new Zend_Validate_Exception('No database adapter present');
+            }
+        }
         return $this->_adapter;
     }
 
@@ -255,43 +275,72 @@ abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract
     }
 
     /**
-     * Run query and returns matches, or null if no matches are found.
-     *
-     * @param  String $value
-     * @return Array when matches are found.
+     * Sets the select object to be used by the validator
+     * 
+     * @param Zend_Db_Select $select
+     * @return Zend_Validate_Db_Abstract
      */
-    protected function _query($value)
+    public function setSelect($select)
     {
-        /**
-         * Check for an adapter being defined. if not, fetch the default adapter.
-         */
-        if ($this->_adapter === null) {
-            $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
-            if (null === $this->_adapter) {
-                require_once 'Zend/Validate/Exception.php';
-                throw new Zend_Validate_Exception('No database adapter present');
-            }
+        if (!$select instanceof Zend_Db_Select) {
+            throw new Zend_Validate_Exception('Select option must be a valid ' .
+                                              'Zend_Db_Select object');
         }
+        $this->_select = $select;
+        return $this;
+    }
 
-        /**
-         * Build select object
-         */
-        $select = new Zend_Db_Select($this->_adapter);
-        $select->from($this->_table, array($this->_field), $this->_schema)
-               ->where($this->_adapter->quoteIdentifier($this->_field, true).' = ?', $value);
-        if ($this->_exclude !== null) {
-            if (is_array($this->_exclude)) {
-                $select->where($this->_adapter->quoteIdentifier($this->_exclude['field'], true).' != ?', $this->_exclude['value']);
-            } else {
-                $select->where($this->_exclude);
+    /**
+     * Gets the select object to be used by the validator.
+     * If no select object was supplied to the constructor,
+     * then it will auto-generate one from the given table,
+     * schema, field, and adapter options.
+     *
+     * @return Zend_Db_Select The Select object which will be used
+     */
+    public function getSelect()
+    {
+        if (null === $this->_select) {
+            $db = $this->getAdapter();
+            /**
+             * Build select object
+             */
+            $select = new Zend_Db_Select($db);
+            $select->from($this->_table, array($this->_field), $this->_schema)
+                   ->where(
+                       $db->quoteIdentifier($this->_field, true).' = :value'
+                   );
+            if ($this->_exclude !== null) {
+                if (is_array($this->_exclude)) {
+                    $select->where(
+                          $db->quoteIdentifier($this->_exclude['field'], true) .
+                            ' != ?', $this->_exclude['value']
+                    );
+                } else {
+                    $select->where($this->_exclude);
+                }
             }
+            $select->limit(1);
+            $this->_select = $select;
         }
-        $select->limit(1);
+        return $this->_select;
+    }
 
+    /**
+     * Run query and returns matches, or null if no matches are found.
+     *
+     * @param  String $value
+     * @return Array when matches are found.
+     */
+    protected function _query($value)
+    {
+        $select = $this->getSelect();
         /**
          * Run query
          */
-        $result = $this->_adapter->fetchRow($select, array(), Zend_Db::FETCH_ASSOC);
+        $result = $select->getAdapter()->fetchRow($select,
+                                                array('value' => $value),
+                                                Zend_Db::FETCH_ASSOC);
 
         return $result;
     }