Jump to:

3430 Posts in 1057 Topics by 734 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: 1112 Views
  • snaip
    Avatar
    Community Member
    180 Posts

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

    hi

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

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


    doesnt work

    EDIT:

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

  • Willr
    Avatar
    Forum Moderator
    5489 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.

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