Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Change collation without hacking the core?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 843 Views
  • opex
    Avatar
    Community Member
    7 Posts

    Change collation without hacking the core? Link to this post

    Hi,

    I'm trying to create a DataModel with utf8_swedish_ci insted of the default utf8_general_ci. Is there a way to do this without modifying MySQLDatabase.php which seems to be the villain here?

    My first attempt was to subclass Varchar since that's hard coded to utf8_general_ci and make my own CollatableVarchar which took a collation argument in the constructor and used that in requiredFields() to create a $values accordingly which is sent to DB::requireField().

    But I got to ask. What's the point of that $values array in DBField::requireField() since MySQLDatabase is hard coded all over with "character set utf8 collate utf8_general_ci"?

    So I guess my question is if there's a post hook in the table creation process that I can jack into and clean up that static junk?

    The reason I want utf8_swedish_ci is because characters like åäö is incorrectly sorted in utf8_general_ci.

    Regards
    John

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Change collation without hacking the core? Link to this post

    Welcome to the forums!

    Taking a quick look at the code it looks like it's rather well baked into the core files. You raise some good points, you may want to bring those up on the silverstripe-dev mailing list as it seems like an oversight. The core devs are more likely to be reading the mailing list.

  • opex
    Avatar
    Community Member
    7 Posts

    Re: Change collation without hacking the core? Link to this post

    If anyone's interested I found a way around this.

    In my model I added this little snippet of code:

    public function requireTable() {
       parent::requireTable();
       $conn = DB::getConn();
       $conn->transAlterField('MyTable', 'MyField', 'VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_swedish_ci NULL DEFAULT NULL');
    }

    The downside here is that each /dev/build will render a faulty status message each time about the fields being converted to utf8_general_ci.

    Perhaps there's a way to dig into the schema update in SS_Database and remove the pre-existing none wanted updates. But I've just skimmed the surface to get an automated solution to my problem.

  • lx
    Avatar
    Community Member
    82 Posts

    Re: Change collation without hacking the core? Link to this post

    Thanks opex,

    your code was very helpfull !!

    I used it to get UNSIGNED INT

    public function requireTable() {
    parent::requireTable();
    $conn = DB::getConn();
    $conn->transAlterField('MyTable', 'MyINT', 'INT(11) UNSIGNED NOT NULL DEFAULT 0');
    }

    843 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.