php - Best way to set charset for Zend_Db (or at least better than what I'm currently doing) -


i'm using zend_db , trying change charset utf8, here 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 enough add charset in config, applys if set directly using:

zend_registry::get('db')->query("set names 'utf8'"); zend_registry::get('db')->query("set character set 'utf8'"); 

my question: there better way set charset, maybe config wise?

firstly i'd break out database setup it's own init function 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 predefined config structures common tasks in zend framework. having following in application.ini config file, can address resource 'db' in 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 sqlite db mysql similar, pdo_mysql , dbname not file path string.

more documentation on resources can found here:

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

now using resources config section agin can add following lines set database character set so:

resources.db.params.charset = "utf8" resources.db.params.driver_options.1002 = "set names utf8;" 

if still have problems, take @ rob allen's zend framework tutorial blog post on akrabat dot com , comments sarting @ around number 45 onwards regarding utf8 , mysql setup zf resources.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -