How to properly use UTF-8-encoded data from Schema inside Catalyst app?

寵の児 提交于 2019-12-10 04:28:59

问题


Data defined inside Catalyst app or in templates has correct encoding and is diplayed well, but from database everything non-Latin1 is converted to ?. I suppose problem should be in model class, which is such:

use strict;
use base 'Catalyst::Model::DBIC::Schema';

__PACKAGE__->config(
 schema_class => 'vhinnad::Schema::DB',

 connect_info => {
     dsn => 'dbi:mysql:test',
     user => 'user',
     password => 'password',
     {
         AutoCommit        => 1,
         RaiseError        => 1,
         mysql_enable_utf8 => 1,
     },
     'on_connect_do' => [
             'SET NAMES utf8',
     ],      
     }
);

1;

I see no flaws here, but something must be wrong. I used my schema also with test scripts and data was well encoded and output was correct, but inside Catalyst app i did not get encoding right. Where may be the problem?

EDIT

For future reference i put solution here: i mixed in connect info old and new style.

Old style is like (dsn, username, passw, hashref_options, hashref_other options)

New style is (dsn => dsn, username => username, etc), so right is to use:

 connect_info => {
     dsn               => 'dbi:mysql:test',
     user              => 'user',
     password          => 'password',
     AutoCommit        => 1,
     RaiseError        => 1,
     mysql_enable_utf8 => 1,
     on_connect_do     => [
             'SET NAMES utf8',
     ],      
 }

回答1:


In a typical Catalyst setup with Catalyst::View::TT and Catalyst::Model::DBIC::Schema you'll need several things for UTF-8 to work:

  • add Catalyst::Plugin::Unicode::Encoding to your Catalyst app
  • add encoding => 'UTF-8' to your app config
  • add ENCODING => 'utf-8' to your TT view config
  • add <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/> to the <head> section of your html to satisfy old IEs which don't care about the Content-Type:text/html; charset=utf-8 http header set by Catalyst::Plugin::Unicode::Encoding
  • make sure your text editor saves your templates in UTF-8 if they include non ASCII characters
  • configure your DBIC model according to DBIx::Class::Manual::Cookbook#Using Unicode
  • if you use Catalyst::Authentication::Store::LDAP configure your LDAP stores to return UTF-8 by adding ldap_server_options => { raw => 'dn' }

According to Catalyst::Model::DBIC::Schema#connect_info:

The old arrayref style with hashrefs for DBI then DBIx::Class options is also supported.

But you are already using the 'new' style so you shouldn't nest the dbi attributes:

connect_info => {
     dsn               => 'dbi:mysql:test',
     user              => 'user',
     password          => 'password',
     AutoCommit        => 1,
     RaiseError        => 1,
     mysql_enable_utf8 => 1,
     on_connect_do     => [
         'SET NAMES utf8',
     ],      
}



回答2:


This advice assumes you have fairly up to date versions of DBIC and Catalyst.

  • This is not necessary: on_connect_do => [ 'SET NAMES utf8' ]
  • Ensure the table|column charsets are UTF-8 in your DB. You can achieve things that sometimes look right even when parts are broken. The DB must be saving the character data as UTF-8 if you expect the entire chain to work.
  • Ensure you're using and configuring Catalyst::Plugin::Unicode::Encoding in your Catalyst app. It did have serious-ish bugs in the not too distant past so get the newest.


来源:https://stackoverflow.com/questions/13138698/how-to-properly-use-utf-8-encoded-data-from-schema-inside-catalyst-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!