Jump to:

23376 Posts in 18251 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Get DataObejctSet of pages with ID filter

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

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

Page: 1
Go to End
Author Topic: 1305 Views
  • jacobsjensen
    Avatar
    Community Member
    20 Posts

    Get DataObejctSet of pages with ID filter Link to this post

    Hello Experts,

    I need help! I want a DataObejctSet consisting of a specific type of pages, filtered by their id.
    In other words, i want the query to return 3 pages, all of the type "WithPreviewPage", identified by their id.

    The pages are of the type "WithPreviewPage" and are not necessarily children of the same page.

    The below code will give me all of the pages of type "WithPreviewPage", but i don't know how to filter by ID's.
    I can do DataObject::get("WithPreviewPage", "Sort = 3"); which will give me the record with the sort order 3, but as soon as i attempt to use ID i get an "Website error", no further description.

    class ForsidePage_Controller extends Page_Controller {
       function showPreviews() {
        $records = DataObject::get("WithPreviewPage");//, $filter, $sort, $join, $limit);
        print "<pre>";
        print_r($records);
        print "</pre>";
        return ($records);
       }
    }

    Can anyone please show me how to return the 3 pages of WithPreviewPage with the id's 8,9,10.

    Thank you for your time.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Get DataObejctSet of pages with ID filter Link to this post

    "Website error", no further description.

    Put the site into dev mode so you can see the full error messages. Add Director::set_environment_type("dev"); to your _config file or add ?isDev=1 to the url.

    Try quoting the Page Table Name as well.

    DataObject::get("WithPreviewPage", "`WithPreviewPage`.ID IN ('8,9,10')");

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: Get DataObejctSet of pages with ID filter Link to this post

    Personally I'd just Add a showPreview field to the WithPreviewPage Object that way you could filter a lot easier

    That way your query could look like

    $records = DataObject:get("WithPreviewPage","showPreview=1");

    plus you won't need to edit the code to look for specific IDs if you want to change it later. You just click a check box in the CMS.

  • jacobsjensen
    Avatar
    Community Member
    20 Posts

    Re: Get DataObejctSet of pages with ID filter Link to this post

    Thx willr, adding the table name did the trick.

    Zenmonkey, we did think of this, but we would rather select the pages to show on the homepage, and not on the individual pages.

    Thank you for your answers.

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