Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Get element by DataObject::get ?

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

Page: 1
Go to End
Author Topic: 1124 Views
  • snaip
    Community Member
    180 Posts

    Get element by DataObject::get ? Link to this post


    simple problem
    how to get title from DataObject::get ?

       function a() {
          $x = DataObject::get("SiteTree");
          return $x->Title;

    doesnt work


       function a() {
          $x = DataObject::get("SiteTree");
          foreach($x as $y) {
             echo '<p>' . $y->Title . ' ' . $y->URLSegment . '</p>';

  • Willr
    Forum Moderator
    5508 Posts

    Re: Get element by DataObject::get ? Link to this post

    Explaination - DataObject::get returns a set. When you have a set of objects you can't call methods, fields on the set since they exist on the objects in the set and not the set itself.

    DataObject::get('SiteTree'); - returns a set of all SiteTree objects (pages)
    DataObject::get_one('SiteTree'); - returns a object - the first SiteTree object
    DataObject::get_by_id('SiteTree', $id); - returns a object - the SiteTree object with the ID = $id;

    Hope that helps.

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.