Browse Source

[ZF-6620] Zend_Db

- Allow assign fetch mode via options config.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23192 44c647ce-9c0f-0410-b52a-842ac1e357ba
ramon 15 years ago
parent
commit
f46faa6b16

+ 5 - 0
library/Zend/Db.php

@@ -43,6 +43,11 @@ class Zend_Db
     const CASE_FOLDING = 'caseFolding';
 
     /**
+     * Use the FETCH_MODE constant in the config of a Zend_Db_Adapter.
+     */
+    const FETCH_MODE = 'fetchMode';
+
+    /**
      * Use the AUTO_QUOTE_IDENTIFIERS constant in the config of a Zend_Db_Adapter.
      */
     const AUTO_QUOTE_IDENTIFIERS = 'autoQuoteIdentifiers';

+ 12 - 1
library/Zend/Db/Adapter/Abstract.php

@@ -184,7 +184,8 @@ abstract class Zend_Db_Adapter_Abstract
 
         $options = array(
             Zend_Db::CASE_FOLDING           => $this->_caseFolding,
-            Zend_Db::AUTO_QUOTE_IDENTIFIERS => $this->_autoQuoteIdentifiers
+            Zend_Db::AUTO_QUOTE_IDENTIFIERS => $this->_autoQuoteIdentifiers,
+            Zend_Db::FETCH_MODE             => $this->_fetchMode,
         );
         $driverOptions = array();
 
@@ -236,6 +237,16 @@ abstract class Zend_Db_Adapter_Abstract
             }
         }
 
+        if (array_key_exists(Zend_Db::FETCH_MODE, $options)) {
+            if (is_string($options[Zend_Db::FETCH_MODE])) {
+                $constant = 'Zend_Db::FETCH_' . strtoupper($options[Zend_Db::FETCH_MODE]);
+                if(defined($constant)) {
+                    $options[Zend_Db::FETCH_MODE] = constant($constant);
+                }
+            }
+            $this->setFetchMode((int) $options[Zend_Db::FETCH_MODE]);
+        }
+
         // obtain quoting property if there is one
         if (array_key_exists(Zend_Db::AUTO_QUOTE_IDENTIFIERS, $options)) {
             $this->_autoQuoteIdentifiers = (bool) $options[Zend_Db::AUTO_QUOTE_IDENTIFIERS];

+ 20 - 0
tests/Zend/Application/Resource/DbTest.php

@@ -203,6 +203,26 @@ class Zend_Application_Resource_DbTest extends PHPUnit_Framework_TestCase
         $resource->init();
         $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache());
     }
+
+    /**
+     * @group ZF-6620
+     */
+    public function testSetOptionFetchMode()
+    {
+        $config = array(
+            'bootstrap' => $this->bootstrap,
+            'adapter' => 'PDO_SQLite',
+            'params'  => array(
+                'dbname'    => ':memory:',
+                'options'   => array(
+                    'fetchMode' => 'obj'
+                )
+            ),
+        );
+        $resource = new Zend_Application_Resource_Db($config);
+        $db = $resource->init();
+        $this->assertEquals($db->getFetchMode(), Zend_Db::FETCH_OBJ);
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_DbTest::main') {

+ 2 - 1
tests/Zend/Db/Adapter/Static.php

@@ -288,7 +288,8 @@ class Zend_Db_Adapter_Static extends Zend_Db_Adapter_Abstract
      */
     public function setFetchMode($mode)
     {
-        return;
+        $this->_fetchMode = $mode;
+        return $this;
     }
 
     /**

+ 27 - 0
tests/Zend/Db/Adapter/StaticTest.php

@@ -380,6 +380,33 @@ class Zend_Db_Adapter_StaticTest extends PHPUnit_Framework_TestCase
 
     }
 
+    /**
+     * @group ZF-6620
+     */
+    public function testDbConstructorSetOptionFetchMode()
+    {
+        $db = new Zend_Db_Adapter_Static(array('dbname' => 'dummy'));
+        $this->assertEquals($db->getFetchMode(), Zend_Db::FETCH_ASSOC);
+
+        $params = array(
+            'dbname' => 'dummy',
+            'options' => array(
+                Zend_Db::FETCH_MODE => 'obj'
+             )
+        );
+        $db = new Zend_Db_Adapter_Static($params);
+        $this->assertEquals($db->getFetchMode(), Zend_Db::FETCH_OBJ);
+
+        $params = array(
+            'dbname' => 'dummy',
+            'options' => array(
+                Zend_Db::FETCH_MODE => Zend_Db::FETCH_OBJ
+             )
+        );
+        $db = new Zend_Db_Adapter_Static($params);
+        $this->assertEquals($db->getFetchMode(), Zend_Db::FETCH_OBJ);
+    }
+
     protected function _isCaseSensitiveFileSystem()
     {
         if (self::$_isCaseSensitiveFileSystem === null) {

+ 16 - 0
tests/Zend/Db/Adapter/TestCommon.php

@@ -2094,4 +2094,20 @@ abstract class Zend_Db_Adapter_TestCommon extends Zend_Db_TestSetup
         unset($stmt);
         $this->_util->dropTable($tableName);
     }
+
+    /**
+     * @group ZF-6620
+     */
+    public function testAdapterOptionFetchMode()
+    {
+        $params = $this->_util->getParams();
+
+        $params['options'] = array(
+            Zend_Db::FETCH_MODE => 'obj'
+        );
+        $db = Zend_Db::factory($this->getDriver(), $params);
+        $select = $db->select()->from('zfproducts');
+        $row = $db->fetchRow($select);
+        $this->assertType('stdClass', $row);
+    }
 }