PDO_PGSQL利用時にclient_encodingを指定する
ちょっと悩んでましたが難しく考えすぎでした。。。
とりあえず、"postgresql.conf内にclient_encodingを定義しておく"というのを除いて、2パターンはありますね。
環境変数"PGCLIENTENCODING"を利用する
httpd起動時に環境変数"PGCLIENTENCODING"が設定されていればOKです。通常はこちらでしょうか。
たとえば、apachectlを実行する前にPGCLIENTENCODINGをexportしておくとか。また、/usr/local/apache2/bin/envversに定義する場合は以下のような感じ。
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # # envvars-std - default environment variables for apachectl # # This file is generated from envvars-std.in # LD_LIBRARY_PATH="/usr/local/apache2/lib:$LD_LIBRARY_PATH" export LD_LIBRARY_PATH # export PGCLIENTENCODING=UTF8
ただし、Webアプリから複数DBを使う、かつ、エンコーディングが変わる(既存のDBと新規DBが混ざってるとか)場合は使えませんね。
config.yml(もしくはparameters.ini)に定義する
接続情報のhost、port、dbnameのいずれかにclient_encodingのオプションを付けてやればOKです。以下の例は昨日のエントリにあるconfig.ymlのdbnameにオプションを付けたものです。
: doctrine: dbal: default_connection: %database_name_0% connections: %database_name_0%: driver: %database_driver% host: %database_host% port: %database_port% # 次のようにシングルクオートがなくてもOK(っぽい) # %database_name_0% options=--client_encoding=UTF8 dbname: %database_name_0% options='--client_encoding=UTF8' user: %database_user% password: %database_password% %database_name_1%: driver: %database_driver% :
このoptionsの書き方は、PHP: pg_connect - Manualの例にあります。
config.ymlのDB接続情報は、Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver以下にある各DBに対応したドライバ内でDSNに変換されますが、その際に利用される項目は先程のhost、port、dbnameになります。ですので、これらのいずれかにオプションを付ける必要があります。
ちなみに、PDO_PGSQL用のDoctrineドライバ(Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php)の場合、Doctrine Configuration Reference (Symfony 2.0 Docs)にある"charset"は無視されるので注意が必要です。
こちらの方法なら昨日のエントリと組み合わせて、エンコーディングが混ざった複数DB環境でも大丈夫そうです。
って、そんな環境あるんかな。。。歴代のレガシーシステムのDBに接続しなきゃならないWebアプリとか?あー。。。orz