gpt4 book ai didi

mysql - Symfony+推进 : Setting connection collation

转载 作者:行者123 更新时间:2023-11-29 00:35:49 29 4
gpt4 key购买 nike

我正在通过 MySQL Server 5.5.28 使用与 Propel 1.6.7 集成的 Symfony 2.1.6 框架作为数据库 ORM,我在将数据库排序规则设置为 utf8_unicode_ci 时遇到问题。 .

我的app/config/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: {}

(参数已经定义在app/config/parameters.yml)

没有options它可以工作,但有字符集问题(MySQL 默认为 utf8_general_ci 排序规则,但数据库模式是用 utf8_unicode_ci 定义的)。如果我使用 options如上所述,Propel/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 中?

最佳答案

如果你看到源代码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')' }

仅供引用:https://github.com/propelorm/PropelBundle/blob/1.1/Resources/doc/configuration.markdown

Propel runtime configuration file

关于mysql - Symfony+推进 : Setting connection collation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14347709/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com