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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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   2317 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.

Avatar
Andante

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.