Parcourir la source

ZF-11304
Ensure charset information is included in the PDO dsn for mysql


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23985 44c647ce-9c0f-0410-b52a-842ac1e357ba

ralph il y a 14 ans
Parent
commit
83ceb74071
2 fichiers modifiés avec 31 ajouts et 0 suppressions
  1. 13 0
      library/Zend/Db/Adapter/Pdo/Mysql.php
  2. 18 0
      tests/Zend/Db/Adapter/Pdo/MysqlTest.php

+ 13 - 0
library/Zend/Db/Adapter/Pdo/Mysql.php

@@ -77,6 +77,19 @@ class Zend_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Abstract
     );
 
     /**
+     * Override _dsn() and ensure that charset is incorporated in mysql
+     * @see Zend_Db_Adapter_Pdo_Abstract::_dsn()
+     */
+    protected function _dsn()
+    {
+        $dsn = parent::_dsn();
+        if (isset($this->_config['charset'])) {
+            $dsn .= ';charset=' . $this->_config['charset'];
+        }
+        return $dsn;
+    }
+    
+    /**
      * Creates a PDO object and connects to the database.
      *
      * @return void

+ 18 - 0
tests/Zend/Db/Adapter/Pdo/MysqlTest.php

@@ -307,9 +307,27 @@ class Zend_Db_Adapter_Pdo_MysqlTest extends Zend_Db_Adapter_Pdo_TestCommon
         $this->assertEquals(1, $result[0]['product_id']);
     }
 
+    /**
+     * @group ZF-11304
+     */
+    public function testAdapterIncludesCharsetInsideGeneratedPdoDsn()
+    {
+        $adapter = new ZendTest_Db_Adapter_Pdo_Mysql(array('dbname' => 'foo', 'charset' => 'XYZ', 'username' => 'bar', 'password' => 'foo'));
+        $this->assertEquals('mysql:dbname=foo;charset=XYZ', $adapter->_dsn());
+    }
+    
     public function getDriver()
     {
         return 'Pdo_Mysql';
     }
 
 }
+
+class ZendTest_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql
+{
+    public function _dsn()
+    {
+        return parent::_dsn();
+    }
+}
+