PHP Doctrine - Best Practice for Setting Default Encoding for All Applications

At the moment, I set my character set and collation as follows:

class Model extends Doctrine_Record
{

  public function setTableDefinition()
  {
    //...

    $this->option('collate', 'utf8_unicode_ci');
    $this->option('charset', 'utf8');
  }

}

I set this in all the definitions of my tables. Is there any way to set the default value? In my bootstrap, I set other defaults:

Doctrine_Manager::getInstance()->setAttribute(
    Doctrine::ATTR_DEFAULT_IDENTIFIER_OPTIONS,
    array('name' => 'id', 'type' => 'integer', 'length' => 4));

It would be nice if there was a way to do the same for collate and charset. I found constants for it, but could not find where / when they were ever used.

thank

+3
source share
2 answers
Doctrine_Manager::connection('mysql://user:pass@host/schema')
    // This must be the charset of your HTTP header 
    // header('Content-Type: text/html;charset=utf-8');
    // or your HTML Head tag
    // <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    ->setCharset('UTF8')
    ;

This is all you need (even with Latin tables, for example, e, for example, will be saved correctly).

, , (DB, HTML), (, , , latin1)

+3

, (Mysql, MSSql, SQLite,...). , :

1:

my.cf. /etc/mysql/my.cnf Debian (Ubuntu) ( , os).

[mysqld]
default-character-set = utf8
default-character-collate = utf8_unicode_ci

mysql. .

2: :

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

:

ALTER DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

3: :

ALTER TABLE `tablename` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

4: - :

public function configureDoctrine(Doctrine_Manager $manager)
{
    $manager->setCollate('utf8_unicode_ci');
    $manager->setCharset('utf8');
}
+2

Source: https://habr.com/ru/post/1766138/


All Articles