Alba HK

Chinese Characters with Propel ORM

After walking through the Propel ORM tutorial on building a project my app could not display Chinese characters (UTF-8) correctly. Instead of characters I was getting “????”. I confirmed the columns in the DB were in UTF-8 format and viewed the data in another program – it checked out ok. I double checked the database encoding/collation and it was correctly set to UTF-8/Unicode. The individual table and column were also set for UTF-8 – its important to check this since some MySQL installations may default to latin1 which will not work with any non-english characters.

The solution for my problem turned out to be adding a few lines in the runtime-conf.xml file. The build tutorial in the Propel tutorial shows a standard runtime-conf.xml file but does not include an optional configuration parameter for the encoding on the DB connection. Below is the standard file with the encoding option added for UTF8 in bold:

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <!-- Uncomment this if you have PEAR Log installed
  <log>
    <type>file</type>
    <name>/path/to/propel.log</name>
    <ident>propel-bookstore</ident>
    <level>7</level>
  </log>
  -->
  <propel>
    <datasources default="bookstore">
      <datasource id="bookstore">
        <adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
        <connection>
          <dsn>mysql:host=localhost;dbname=my_db_name</dsn>
          <user>my_db_user</user>
          <password>my_db_password</password>
          <settings>
            <setting id="charset">utf8</setting>
          </settings>
        </connection>
      </datasource>
    </datasources>
  </propel>
</config>

After the making the above changes I could get Chinese characters correctly displaying in my app.

Leave a Reply

Your email address will not be published. Required fields are marked *