Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Query about casting

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

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

Page: 1
Go to End
Author Topic: 1905 Views
  • Sigurd
    Avatar
    Forum Moderator
    628 Posts

    Query about casting Link to this post

    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.

  • Andy
    Avatar
    230 Posts

    Re: Query about casting Link to this post

    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.

    1905 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.