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, biapar, Willr, Ingo, simon_w

Query about casting


Reply


2 Posts   1977 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
Andy

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