Zendcasts Forum

A community of developers who work with the Zend Framework and other enterprise PHP technologies

You are not logged in.

#1 2010-11-18 18:22:23

Jarsäter
Member
Registered: 2010-08-25
Posts: 11

Zend + Doctrine, Charset problems

Recently i ran in to a problem with my charset when i used the Zend and Doctrine libraries to develop my project. I have build a form with Zend_Form and then I use this code to save the data to the database:

<?php
public function addAction() {
    if ($this->getRequest()->isPost()) {
        if ($this->frm->isValid($this->_getAllParams())) {
            $name = $this->_getParam('name');
            $desc = $this->_getParam('description');
            $acti = $this->_getParam('active') == 1 ? 1 : 0;
            $seas = $this->_getParam('season');

            $recepie = new Model_Recepie(null, true);
            $recepie->name        = $name;
            $recepie->description = $desc;
            $recepie->season      = $seas;
            $recepie->active      = $acti;
            $recepie->save();         
        } // Form is valid
    } //Request is POST
    $this->view->frm = $this->frm;
}

As you can see, it is quite simple. And when i print it in my view-files it works perfectly.

But, when you really dig in to it and check the database and look at the Recepie-table the swedish characters ÅÄÖ looks like åäö.

I solved this problem by adding two lines of code in my Bootstrap.php

<?php
protected function _initDoctrine() {
    /** [...] **/

    $conn = Doctrine_Manager::connection ( $doctrineConfig ['dsn'], 'doctrine' );
    $conn->setAttribute ( Doctrine::ATTR_USE_NATIVE_ENUM, true );
    $conn->setCharset ( 'utf8' );
    $conn->setCollate ( 'utf8_unicode_ci' );
    return $conn;
}

As you can se, it's a pretty simple fix.

Offline

#2 2011-05-24 20:27:07

toma42
New member
Registered: 2011-05-24
Posts: 1

Re: Zend + Doctrine, Charset problems

It sounds like your database table is latin1 so you were handling the data as utf8 but storing it as latin1 which would cause a problem when you start reading it again.

Your setcharset and setcollate is a hack which works around this oversight.

Offline

#3 2011-05-25 06:34:59

Jarsäter
Member
Registered: 2010-08-25
Posts: 11

Re: Zend + Doctrine, Charset problems

My table is is utf-8

Offline

Board footer

Powered by FluxBB