Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Using DataObject::get() to list last few updated pages that are publicly visible

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

Page: 1
Go to End
Author Topic: 2873 Views
  • HansR
    Avatar
    Community Member
    140 Posts

    Using DataObject::get() to list last few updated pages that are publicly visible Link to this post

    Hi, I have a homepage class that displays the 10 most recently updated pages on the website. The problem is, that this includes pages that are not publicly visible. The code is:

    function RecentlyUpdated() {
    $pages = DataObject::get("Page", "", "LastEdited DESC", "", 10);
    $doSet = new DataObjectSet();
    foreach($pages as $key => $data) {
    $title = $data->Title;
    $summary = $data->obj('Content')->FirstParagraph('html');;
    $record = array(
    'Link' => $data->Link(),
    'Title' => $title,
    'Date' => date('Y-m-d', strtotime($data->LastEdited)),
    'Summary' => $summary
    );
    $doSet->push(new ArrayData($record));
    }
    return $doSet;
    }

    What should I set the DataObject::get()'s parameters to in order to obtain just the publicly visible pages. Actually, I'd like to filter out and Forum page too. I assume that I only need to change the filter string, but I have no idea what to set it to.

    Hans

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Using DataObject::get() to list last few updated pages that are publicly visible Link to this post

    Hi HansR,

    I use this to only include visible pages:

    $pages = DataObject::get("Page", "ShowInMenus = 1", "LastEdited DESC", "", 10);

    To filter Forum pages out as well, something like this should work:

    $pages = DataObject::get("Page", "ShowInMenus = 1 AND ClassName !=ForumPage", "LastEdited DESC", "", 10);

  • HansR
    Avatar
    Community Member
    140 Posts

    Re: Using DataObject::get() to list last few updated pages that are publicly visible Link to this post

    Hi NickJacobs,

    Thanks for that. I'm still hoping that someone knows how to get the "show to all" pages, because I might want to include a page in the menu. However, your solution should be fine for now.

    Hans

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Using DataObject::get() to list last few updated pages that are publicly visible Link to this post

    Hello,

    Have you looked at how SilverStripe does this? You might want to look at methods Children(), AllChildren(), stageChildren() and liveChildren() in class Hierarchy.

    Ben

  • HansR
    Avatar
    Community Member
    140 Posts

    Re: Using DataObject::get() to list last few updated pages that are publicly visible Link to this post

    Hi Ben,

    The methods that you pointed to aren't the best option in this case; the get() method is the right one to find the ten most recently updated public pages. Plus, all of the pages are "live." The difference between pages is that most are "viewable by all," whereas some will be "viewable by group X." So, I'm looking to filter out the pages that have restrictive security settings, and forum pages).

    Hans

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Using DataObject::get() to list last few updated pages that are publicly visible Link to this post

    If you want to check permissions then it would be easier to do this in the ORM. Tricky thing is you might have to get more then 10 as you are removing objects as you go. In this one I get all the pages (just for kicks) then adding to a list, making sure I only get 10.

    $pages = DataObject::get("Page", "ShowInMenus = 1 AND ClassName !=ForumPage", "LastEdited DESC");
    $output = new DataObjectSet();
    if($pages) {
    foreach($pages as $page) {
    if($page->canView()) { // check we cant view
    $output->push($page);
    if($output->TotalItems() > 9) break;
    }
    }
    }
    return $output;

  • HansR
    Avatar
    Community Member
    140 Posts

    Re: Using DataObject::get() to list last few updated pages that are publicly visible Link to this post

    @willr

    That almost worked. My code ended up being:

    $pages = DataObject::get("Page", "ShowInMenus = 1 AND ClassName != 'Forum'", "LastEdited DESC");
    if($pages) {
    $output = new DataObjectSet();
    foreach($pages as $page) {
    if($page->canView()) { // check we cant view
    $output->push($page);
    if($output->TotalItems() > 9)
    break;
    }
    }
    }
    return $output;

    The one change is ForumPage changed to 'Forum'. Thanks for your help.

    Hans

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