为 Zend_Db 设置字符集的最佳方法(或者至少比我目前正在做的更好)

发布于 2024-10-03 09:36:14 字数 1104 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场信仰旅途 2024-10-10 09:36:14

首先,我会将数据库设置分解为它自己的 init 函数,如下所示:

/**
 * Initiate Zend Autoloader  
 * @return Zend_Db_Adapter 
 */
protected function _initDatabase() 
{
    $resource = $this->getPluginResource('db');
    $db = $resource->getDbAdapter();
    Zend_Registry::set("db", $db);
    return $db;
}

上面的示例使用的资源是 Zend Framework 中某些常见任务的预定义配置结构。只需在我的 application.ini 配置文件中添加以下内容,我们就可以在上面的 Bootstrap 中寻址资源“db”:

resources.db.adapter = "pdo_sqlite"
resources.db.params.host = "localhost"
resources.db.params.username = "databaseuser"
resources.db.params.password = "mysecretpassword"
resources.db.params.dbname = APPLICATION_PATH "/data/db/ccymod.db"
resources.db.isDefaultTableAdapter = true

此示例适用于 sqlite 数据库,但 MySQL 看起来类似,但使用 pdo_mysql 且 dbname 不会是一个文件路径而是一个字符串。

有关资源的更多文档可以在这里找到:

http://framework .zend.com/manual/en/zend.application.available-resources.html

现在使用资源配置部分 agin,我们可以向其中添加以下行来设置数据库字符集,如下所示:

resources.db.params.charset = "utf8"
resources.db.params.driver_options.1002 = "SET NAMES utf8;"

如果您仍然有问题,请查看 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:

/**
 * Initiate Zend Autoloader  
 * @return Zend_Db_Adapter 
 */
protected function _initDatabase() 
{
    $resource = $this->getPluginResource('db');
    $db = $resource->getDbAdapter();
    Zend_Registry::set("db", $db);
    return $db;
}

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:

resources.db.adapter = "pdo_sqlite"
resources.db.params.host = "localhost"
resources.db.params.username = "databaseuser"
resources.db.params.password = "mysecretpassword"
resources.db.params.dbname = APPLICATION_PATH "/data/db/ccymod.db"
resources.db.isDefaultTableAdapter = true

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:

resources.db.params.charset = "utf8"
resources.db.params.driver_options.1002 = "SET NAMES utf8;"

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.

笑脸一如从前 2024-10-10 09:36:14

取自我们的 application.ini:

db.params.driver_options.3 = "SET NAMES 'utf8'"

Taken from our application.ini:

db.params.driver_options.3 = "SET NAMES 'utf8'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文