为 Zend_Db 设置字符集的最佳方法(或者至少比我目前正在做的更好)
我正在使用 Zend_DB 并尝试将字符集更改为 utf8,这里是代码:
config.ini :
[development]
db.host = "localhost"
db.username = "root"
db.password = "toor"
db.dbname = "db_whoopdiedo"
db.charset = "utf8"
bootstrap.php :
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initAutoload()
{
Zend_Registry::set(
'config',
new Zend_Config_Ini(APPLICATION_PATH.'/configs/config.ini', 'development')
);
Zend_Registry::set(
'db',
Zend_Db::factory('Pdo_Mysql', Zend_Registry::get('config')->db)
);
Zend_Registry::get('db')->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::get('db')->query("SET NAMES 'utf8'");
Zend_Registry::get('db')->query("SET CHARACTER SET 'utf8'");
}
}
我认为在配置中添加字符集就足够了,但他只有在我设置它时才应用它直接使用:
Zend_Registry::get('db')->query("SET NAMES 'utf8'");
Zend_Registry::get('db')->query("SET CHARACTER SET 'utf8'");
我的问题:有没有更好的方法来设置字符集,也许是明智的配置?
I'm using Zend_DB and trying to change the charset to utf8, here is the code:
config.ini :
[development]
db.host = "localhost"
db.username = "root"
db.password = "toor"
db.dbname = "db_whoopdiedo"
db.charset = "utf8"
bootstrap.php :
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initAutoload()
{
Zend_Registry::set(
'config',
new Zend_Config_Ini(APPLICATION_PATH.'/configs/config.ini', 'development')
);
Zend_Registry::set(
'db',
Zend_Db::factory('Pdo_Mysql', Zend_Registry::get('config')->db)
);
Zend_Registry::get('db')->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::get('db')->query("SET NAMES 'utf8'");
Zend_Registry::get('db')->query("SET CHARACTER SET 'utf8'");
}
}
i thought it would be enough to add the charset in the config, but he only applys it if i set it directly using:
Zend_Registry::get('db')->query("SET NAMES 'utf8'");
Zend_Registry::get('db')->query("SET CHARACTER SET 'utf8'");
My Question: is there a better way to set the charset, maybe config wise?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我会将数据库设置分解为它自己的 init 函数,如下所示:
上面的示例使用的资源是 Zend Framework 中某些常见任务的预定义配置结构。只需在我的 application.ini 配置文件中添加以下内容,我们就可以在上面的 Bootstrap 中寻址资源“db”:
此示例适用于 sqlite 数据库,但 MySQL 看起来类似,但使用 pdo_mysql 且 dbname 不会是一个文件路径而是一个字符串。
有关资源的更多文档可以在这里找到:
http://framework .zend.com/manual/en/zend.application.available-resources.html
现在使用资源配置部分 agin,我们可以向其中添加以下行来设置数据库字符集,如下所示:
如果您仍然有问题,请查看 Rob Allen 在 akrabat dot com 上的 zend 框架教程博客文章,以及从第 45 号左右开始的有关 ZF 资源的 UTF8 和 mysql 设置的评论。
Firstly I'd break out your database setup into it's own init function like so:
The above example uses resources which are predefined config structures for certain common tasks in Zend Framework. Simply by having the following in my application.ini config file, we can address the resource 'db' in the Bootstrap above:
This example is for an sqlite db but MySQL would look similar, but with pdo_mysql and the dbname would not be a file path but a string.
More documentation on the resources can be found here:
http://framework.zend.com/manual/en/zend.application.available-resources.html
Now using the resources config section agin we can add to it the following lines to set the database character set like so:
If you still have problems, take a look at Rob Allen's zend framework tutorial blog post on akrabat dot com and the comments sarting at around number 45 onwards regarding the UTF8 and mysql setup for ZF resources.
取自我们的 application.ini:
Taken from our application.ini: