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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

DataObject Helper method


Go to End


2 Posts   1026 Views

Avatar
dylangrech92

Community Member, 17 Posts

16 April 2014 at 11:56pm

Hi Guys,

I would love your thoughts about these 2 helper methods. They make the code a little shorter and cleaner, but do you think they make sense? Are they worth it?

class Page{

    public static function get_one( $class = false, $filter = null, $cache = true, $orderby = null ){
        return DataObject::get_one( ( $class ) ? $class : get_called_class(), $filter, $cache, $orderby );
    }
	
    public static function get_by_id( $id, $class = false, $cache = true ){
        return DataObject::get_by_id( ( $class ) ? $class : get_called_class(), $id, $cache );
    }

...
}

To call them I use:

    MyPage::get_by_id( 5 );
    MyPage::get_one();

Rather then:

    DataObject::get_by_id( 'MyPage' , 5 );
    // OR
    MyPage::get_by_id( 'MyPage', 5 );

Avatar
Willr

Forum Moderator, 5523 Posts

23 April 2014 at 4:36pm

In SS3 with the new ORM you could write that as MyPage::get()->byId(3);