2013-01-15 64 views
0

我正在使用與Propel 1.6.7集成的Symfony 2.1.6框架作爲數據庫ORM,通過MySQL服務器5.5.28,我有問題需要將數據庫整理設置爲utf8_unicode_ciSymfony + Propel:設置連接整理

應用程序/配置/ config.yml

... 
propel: 
    dbal: 
     driver:    %database_driver% 
     user:     %database_user% 
     password:    %database_password% 
     dsn:     "%database_driver%:host=%database_host%;dbname=%database_name%;charset=%database_charset%" 
     options: 
      MYSQL_ATTR_INIT_COMMAND: "SET NAMES utf8 COLLATE utf8_unicode_ci" 
     attributes:   {} 

沒有options它的工作原理,但字符集(參數在應用程序/配置/ parameters.yml已定義)問題(MySQL默認爲utf8_general_ci歸類,但數據庫模式定義爲utf8_unicode_ci)。如果我使用options作爲波輪/ Symfony的上面提高了一堆異常:

in /var/www/vendor/propel/propel1/runtime/lib/Propel.php at line 725 -+ 
at ErrorHandler ->handle ('2', 'Illegal string offset 'value'', '/var/www/vendor/propel/propel1/runtime/lib/Propel.php', '725', array('source' => array('MYSQL_ATTR_INIT_COMMAND' => 'SET NAMES utf8 COLLATE utf8_unicode_ci'), 'write_to' => array(), 'option' => 'MYSQL_ATTR_INIT_COMMAND', 'optiondata' => 'SET NAMES utf8 COLLATE utf8_unicode_ci', 'key' => '1002')) 
in /var/www/vendor/propel/propel1/runtime/lib/Propel.php at line 725 -+ 
at Propel ::processDriverOptions (array('MYSQL_ATTR_INIT_COMMAND' => 'SET NAMES utf8 COLLATE utf8_unicode_ci'), array()) 
in /var/www/vendor/propel/propel1/runtime/lib/Propel.php at line 668 -+ 
at Propel ::initConnection (array('dsn' => 'mysql:host=127.0.0.1;dbname=symfony;charset=utf8', 'user' => 'root', 'password' => '********', 'classname' => 'DebugPDO', 'options' => array('MYSQL_ATTR_INIT_COMMAND' => 'SET NAMES utf8 COLLATE utf8_unicode_ci'), 'attributes' => array(), 'settings' => array()), 'default') 
in /var/www/vendor/propel/propel1/runtime/lib/Propel.php at line 576 -+ 
at Propel ::getMasterConnection ('default') 
in /var/www/vendor/propel/propel1/runtime/lib/Propel.php at line 602 -+ 
at Propel ::getSlaveConnection ('default') 
in /var/www/vendor/propel/propel1/runtime/lib/Propel.php at line 552 -+ 
at Propel ::getConnection ('default', 'read') 
in /var/www/vendor/propel/propel1/runtime/lib/query/ModelCriteria.php at line 1160 -+ 
at ModelCriteria ->find() 
in /var/www/src/Acme/AppBundle/Controller/DefaultController.php at line 20 -+ 
at DefaultController ->fooAction() 
at call_user_func_array (array(object(DefaultController), 'fooAction'), array()) 
in kernel.root_dir/bootstrap.php.cache at line 1426 -+ 
at HttpKernel ->handleRaw (object(Request), '1') 
in kernel.root_dir/bootstrap.php.cache at line 1390 -+ 
at HttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/bootstrap.php.cache at line 1566 -+ 
at HttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/bootstrap.php.cache at line 617 -+ 
at Kernel ->handle (object(Request)) 
in /var/www/web/app_dev.php at line 25 -+ 

我知道,我可以改變my.cnf中直接從服務器配置定義MySQL的默認排序規則,但我需要將此設置爲運行時...我錯過了什麼嗎?我應該如何設置MYSQL_ATTR_INIT_COMMANDapp/config/config.yml

回答

1

如果你看到源代碼here

$value = $optiondata['value']; // this is line no 275 
if (is_string($value) && strpos($value, '::') !== false) { 
    if (!defined($value)) { 
     throw new PropelException("Invalid PDO option/attribute value specified: ".$value); 
    } 
    $value = constant($value); 
} 

其預期的‘在選項數據值’鍵,所以嘗試這

propel: 
    dbal: 
     driver:    %database_driver% 
     user:     %database_user% 
     password:    %database_password% 
     dsn:     "%database_driver%:host=%database_host%;dbname=%database_name%;charset=%database_charset%" 
     options: 
      MYSQL_ATTR_INIT_COMMAND: { value: "SET NAMES utf8 COLLATE utf8_unicode_ci" } 
     attributes:   {} 

或者

,還可以定義查詢,其中有被執行後,立即打開連接

propel: 
    dbal: 
     default_connection:   default 
     connections: 
      default: 
       # ... 
       options: 
        ATTR_PERSISTENT: false 
       attributes: 
        ATTR_EMULATE_PREPARES: true 
       settings: 
        charset:  { value: UTF8 } 
        queries:  { query: 'INSERT INTO BAR ('hey', 'there')' } 

FYI:https://github.com/propelorm/PropelBundle/blob/1.1/Resources/doc/configuration.markdown

Propel runtime configuration file

0

關於你改變my.cnf的重點,如果你要使用的數據庫只需要特定的排序規則設置,那麼我認爲在使用命令「create」創建DB時,最好覆蓋排序規則設置數據庫DB_NAME CHARACTER SET UTF8 COLLATE utf8_unicode_ci「,而不是通過編輯全局改變它的my.cnf