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.

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


2405 Views

Avatar
Sigurd

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
data-types."

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

eg

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

eg

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

var_dump($myObj->myflag);

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.