views:

3859

answers:

6

How to make PDO adapter run SET NAMES utf8 each time I connect, In ZendFramework. I am using an INI file to save the adapter config data. what entries should I add there?

If it wasn't clear, I am looking for the correct syntax to do it in the config.ini file of my project and not in php code, as I regard this part of the configuration code.

+3  A: 

fear my google-fu

$pdo = new PDO(
    'mysql:host=mysql.example.com;dbname=example_db',
    "username",
    "password",
    array(PDO::MYSQL\_ATTR\_INIT\_COMMAND => "SET NAMES utf8"));

first hit ;)

SchizoDuckie
:-) My google foo is better, found this before posting, should have sayed so.I will refine my question, I am trying to do it in the config.ini and not in php code.
Itay Moav
A: 

In your bootstrap file...

$db = Zend_Db::factory($adapter, $config);
$db->query("SET NAMES 'utf8'");

then you save this instance in your registry

Zend_Registry::set('db', $db);
patchinko
+8  A: 

Itay,

A very good question. Fortunately for you the answer is very simple:

database.params.driver_options.1002 = "SET NAMES utf8"

1002 is the value of constant PDO::MYSQL_ATTR_INIT_COMMAND

You can't use the constant in the config.ini

David Caunt
A: 

The connection in zend_db is lazy which means it connects on the first query. if you have a static page with no query's in it it will never even connect - even if it is initialized in you bootstrap file.

so running:

$db->query("SET NAMES 'utf8'");

Is not so smart. Big thanks to dcaunt for his solution.

Leon
+3  A: 

just put this in your config

database.params.charset = "utf8"

that is it

tawfekov
This is now the best way to do it :)
David Caunt
Thanks for the badge :) dcaunt
tawfekov
A: 

$table->getAdapter()->query('SET NAMES UTF8');

wormhit