SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Snippets - Little bits of code to make you happy

Getting objects from multiple child pages

When you want to create a DataObjectSet encompasing objects from all the current pages children you can do something like this

$pageIDs = $this->getDescendantIDList();
$ObjectSet = DataObject::get(
                 "Object", 
                 "`ObjectPageID` IN (" . implode(",", $pageIDs) . ")"
                  );

The first line creates an array of the ID's from all, with the current page and any descendant page ID's.

The second line onwards then gets all Objects that have an ObjectPageID that is either the current page's ID, or any child page of the current page.

You can then add the usual filter, join or limit clauses to the DataObject::get call.

Hamish Campbell avatar

Hamish Campbell

Hamish is a web developer for Opus International Consultants in New Zealand. He creates tools for managing data online, with a particular interest in geographical information systems. He is also an active contributor to the SilverStripe open source project.

  • James Barnsley
    16/07/2012 2:29am (2 years ago)

    Hi Hamish,

    Super-handy to know this tidy snippet. Have you had problems loading multiple filters for the one GET? Ie:

    $ObjectSet = DataObject::get(
    'Object',
    'ShowPage = 1 AND (ObjectPageID = 1 OR ObjectPageID = 2)'
    );

    I haven't managed to get this syntax working properly and am wondering if it is at all possible.

Post a comment ...

You cannot post comments until you have logged in. Login Here.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image Ty Barho
6 article image njorndare
7 article image Martijn van Nieuwenhoven
8 article image Darren-Lee
9 article image Roman Schmid
10 article image Matt Clegg

View full leaderboard


Advertisement