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.

Archive /

Our old forums are still available as a read-only archive.

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

Query about casting

Go to End

2 Posts   2315 Views


Forum Moderator, 628 Posts

7 January 2008 at 12:01pm

Here's a question from Tony Rigden who emailed me over the Christmas break.

In the manual under datamodel casting there is this statement
"Properties defined in static $db are automatically casted to their

However, I am finding in my data object that whenever I read a property,
it is always returned as a string


class MyObj extends DataObject{
static $db = array('myflag' => 'Boolean');

Works ok in forms etc but when I go to access it's values in code


$myObj = MyObj::get_by_id("MyObj", 1);


It always shows: string(1) "0" or string(1) "1" depending of the value
in the database

I would have thought the word "automatically" meant you do not need to
use the static $casting array if you do not want to change the type. Is
this correct or do I have to put in the $casting array as well,
matching up the types in the $db array.


Community Member, 231 Posts

7 January 2008 at 2:20pm

The fields are automatically cast in SilverStripe templates, but not if you access their values directly. I'm not entirely sure, but I believe if you use $myobj->getField('myflag') it'll return what you want.