Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Change collation without hacking the core?


Go to End


5 Posts   2614 Views

Avatar
opex

Community Member, 7 Posts

27 September 2011 at 3:34am

Edited: 27/09/2011 3:35am

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

Avatar
Willr

Forum Moderator, 5523 Posts

27 September 2011 at 7:33pm

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.

Avatar
opex

Community Member, 7 Posts

28 September 2011 at 10:05pm

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.

Avatar
lx

Community Member, 83 Posts

29 September 2011 at 10:50pm

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');
}

Avatar
Rodskagg

Community Member, 26 Posts

24 August 2016 at 2:59am

I'm also having this problem, 5 years later. I need utf8_swedish_ci, which I change it to in the database. But everytime I do a dev/build Silverstripe sets it back to uft8_general_ci.