English 中文(简体)
Zend_Db_Table UTF-8 characteristics
原标题:Zend_Db_Table UTF-8 Characters

我的数据库中的表是使用正确的UTF-8字符集创建的,类似于这样:

CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
...
...
...
...
...
PRIMARY KEY (id)
) ENGINE = INNODB  CHARACTER SET utf8 COLLATE utf8_slovak_ci;

然而,当我使用Zend_Db_Table使用此方法从表中获取数据时:

public function getSingle($id)
{
    $select = $this->select();
    $where = $this->getAdapter()->quoteInto( id = ? , $id,  INTEGER );
    $select->where($where);
    return $this->fetchRow($select);
}

它返回一个带有破损UTF-8字符的对象(我猜转换为iso-8859-1)。

当我通过phpmyadmin查看数据库时,它会正确显示所有字符并显示正确的编码(UTF-8),所以我不知道问题出在哪里。

我该如何解决这个问题?

更新:

所以我做了这个,它有效:

protected function _initDb()
{
    $this->configuration = new Zend_Config_Ini(APPLICATION_PATH
                                               .  /configs/application.ini ,
                                               APPLICATION_ENVIRONMENT);
    $this->dbAdapter = Zend_Db::factory($this->configuration->database);
    Zend_Db_Table_Abstract::setDefaultAdapter($this->dbAdapter);
    $stmt = new Zend_Db_Statement_Pdo($this->dbAdapter,
                                      "SET NAMES  utf8 ");
    $stmt->execute();
}

是否有更好的办法?

更新2:

我尝试过这个。

protected function _initDb()
{
    $this->configuration = new Zend_Config_Ini(APPLICATION_PATH
                                               .  /configs/application.ini ,
                                               APPLICATION_ENVIRONMENT);
    $this->dbAdapter = Zend_Db::factory($this->configuration->database);
    $this->dbAdapter = Zend_Db::factory($this->configuration->database->adapter,
                                        $this->configuration->database->params->toArray()
                                        + array( driver_options  => array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES  utf8 ")));
    Zend_Db_Table_Abstract::setDefaultAdapter($this->dbAdapter);
}

我犯了一个错误:

Fatal error: Undefined class constant  MYSQL_ATTR_INIT_COMMAND  in C:wampwwwakalarkaapplicationBootstrap.php on line 46
最佳回答

我猜你在使用MySQL? 我所知道的唯一强制返回UTF-8数据的方法是打开DB连接并运行以下查询:

SET NAMES  utf8 

或者,如果您正在使用mysqli扩展:

mysqli_set_charset( utf8 );

应该也能工作...

问题回答

不必设置 driver_options,你可以轻松地执行以下操作。

你的 ini 文件:

db.adapter         = Pdo_Mysql
db.params.host     = localhost
db.params.dbname   = mydb
db.params.username = myuser
db.params.password = mypass
db.params.charset  = UTF8

Note last parameter. Then read your config using Zend_Config_Ini:

$config = new Zend_Config_Ini( application.ini );

将数据库参数作为配置子对象传递:

$db = Zend_Db::factory($config->db);

Wim是正确的。但是,有一种方法可以在不显式执行查询的情况下完成此操作,即使用Zend_Db工厂方法的driver_options选项。我假设您使用pdo_mysql适配器,并使用Zend_Db的工厂方法进行实例化。

您可以使用此工厂方法将附加选项传递给适配器。您要查找的 pdo_mysql 选项是 PDO 的 MYSQL_ATTR_INIT_COMMAND

这是它的工作原理:

$adapterType =  pdo_mysql ;
$adapterConfig = array(
     host  =>  localhost ,
     dbname  =>  yourdb ,
     username  =>  user ,
     password  =>  yourpassword ,
    // here is where the driver options are defined, as an associative array
     driver_options  => array(
        PDO::MYSQL_ATTR_INIT_COMMAND =>  SET NAMES utf8 
    )
);

$dbAdapter = Zend_Db::factory( $adapterType, $adapterConfig );

正如您所看到的,我们能够利用PDO的MYSQL_ATTR_INIT_COMMAND选项。您可能明白,在.ini配置中,这个常量是无法使用的。因此,如果您从.ini文件中读取数据库适配器配置设置,您要么必须在.ini文件中使用常量的具体值,要么在执行时将driver_options数组追加/合并到工厂方法参数中(这是我所做的),就像这样:

$dbAdapter = Zend_Db::factory(
 $configuration->database->adapter,
 $configuration->database->params->toArray() +
        array(  driver_options  => array( PDO::MYSQL_ATTR_INIT_COMMAND =>  SET NAMES utf8  ) )
);

EDIT
I assume this is perfectly possible with only passing one argument to the factory method, like you do in your example, as well. I just accustomed to the habit of passing two arguments to the factory method.





相关问题
Zend 邮件问题,涉及外国char子+ com子

泽斯德邮局在名称被定为具有外国性质(如“保”)和 com(”)的物品时,就放弃了一种例外(因为邮局(邮局)退回假)。 重新提出以下守则。

PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

Link to a specific step in onepage checkout

Is it possible to redirect the browser to nth step in the onepage checkout? If so, how would one go about doing it? I m working on a payment module and have a sort of "cancel" action that i would ...

Tagging with Zend Framework

I m trying to create the same solutions as below, but using a simple MySQL query (instead of the static version used below where the words/tags are implemented in the code). The name of the MySQL ...

dynamicaly adding textboxes to zend_form

I hope that this is a quick question to answer. I am developing a form using Zend_Form, I have a number of Zend_Dojo_Form_Element_Textboxs to add to this form dynamically. These are added from rows ...

热门标签