Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SS3 call a function from different DataObject

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

Page: 1
Go to End
Author Topic: 434 Views
  • voodoochile
    Avatar
    Community Member
    52 Posts

    SS3 call a function from different DataObject Link to this post

    Hi All

    This may be simple but im just not sure

    I have a dataobject extension that has a function with a query in it, i want to access that function from a different dataobject extension.
    Can this be done? and how?

    Thanks

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: SS3 call a function from different DataObject Link to this post

    Create it as a static function:

    Class MyDataObject extends DataObject {

    public static function foo() {
    // Do some querying
    }

    }

    You can then call it by doing:

    MyDataObject::foo()

  • voodoochile
    Avatar
    Community Member
    52 Posts

    Re: SS3 call a function from different DataObject Link to this post

    Hi Kinglozzer

    Thanks for your reply, when i made the function public static the wheels came off a bit and nothing worked.

    although this gives me something to go on unfortunatly i have little time to explore this right now so i will plug away as i get time and try a few things and see what i get working

    thanks again

    voodoochile

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: SS3 call a function from different DataObject Link to this post

    If it's static you need to refer to the extension classname (e.g MyDataExtension::foo()). If your method is instance based you need to go $this->owner->foo().

  • voodoochile
    Avatar
    Community Member
    52 Posts

    Re: SS3 call a function from different DataObject Link to this post

    Thanks Willr

    I managed to work that bit out as in MyDataExtension::foo() and kind of got it working but not well, in the page view it returns a value for each object but dosn't seem to be running the query.

    at this point i think i need to play around a little with this to get the result i am looking for.

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