Function in HomePageController not working

Silverstripe Version: 4.2

Try to retrieve data from an extended Page and use in a function on HomePage

I have a CustomPage and want to show data on the Homepage under certain conditions
In my HomePageController:

public function UpcomingDates(){
   $today = date("Y-m-d");
   $CustomPageData = CustomPage::get();
        Debug::show($CustomPageData); // shows Date1 = 2019-02-24, Date2 = 2019-04-07
  $Date1 = $CustomPageData->Date1;
        Debug::show($Date1);  // doesn't show anything

Why isn’t $CustomPageData->Date1 showing anything?

If I try the function I am trying to get to work in HomePage on the CustomPageController it does work perfectly. What am I missing here?

Thank you very much in advance.

CustomPage::get() will give you a DataList, not a Page. If you want to work on the first Page in the list try $CustomPageData = CustomPage::get()->first().

Perfect, thank you, JonoM!

Right, thanks, JonoM!
Just out of curiosity, what if I wanted to get the second one?

Generally if you wanted to target a specific item you’d filter the list accordingly, so that the only item in the list is the one you want, and then you can still call ->first(). But you can loop over the items in a DataList with a for each. Here’s a little more info:

You can add limits to the query, eg. ->limit(1,1) if you want to pick out specific records from the list.


Hi JonoM and DorsetDigital,

Thank you so much! I learned a lot.
I am so glad I asked the additional question out of curiosity.

Thanks and have a great rest of the week!

Hi DorsetDigital,

I really liked your approach and gave it a go.
According to the description your suggestion ->limit(1,1) would make perfectly sense to get the second page, but in my case I got the first page as a result and ->limit(1,0) was giving me the second page.
Is this a bug in 4.2? I know I should update. I really need to do so, as I have a different problem, my $Date1.Day doesn’t give me the “Monday”, $Date1.Month works fine, but first I should update and this would then be a different topic.
Still, thanks for your help!

Hi there,

I upgraded to version 4.3.3 and for some reason the
$CustomPageData = CustomPage::get()->first() doesn’t work anymore, but that might be as I added a function to the HomePage that requests data from the extended CustomPage (first time that I did this) and now try to get Upcoming dates from the CustomPage as well as from the CustomPageExtendedPage on the HomePage.

public function UpcomingCustomPageExclusionDays(){
        $CustomPageData = CustomPage::get()->first();
        $upcomingExclusionDays = $this->ThisTermUpcomingExclusion($CustomPageData);
        return $upcomingExclusionDays;
public function UpcomingCustomPageExtendedPageExclusionDays(){
    $CustomPageExtendedPageData = CustomPageExtendedPage::get()->First();
    $upcomingExclusionDays = $this->ThisTermUpcomingExclusion($CustomPageExtendedPageData);
    return $upcomingExclusionDays;

In the database there are two entries in CustomPage table. The first is the one from the original CustomPage and the second from the CustomPageExtendedPage. So, I assumed when I do the
$CustomPageData = CustomPage::get()->first() I would get the first entry of the db table, but I get the entry from CustomPageExtendedPage in both cases (even when I comment the UpcomingCustomPageExtendedPageExclusionDays() ).
If I do $CustomPageData = CustomPage::get()->last() I get the results I want.

Is this a bug or is my thinking not right?

I’m not following the code or end goal too well, but assuming that the record you want will be in a particular place in the database makes your code fragile and is probably not a good idea. The point of a CMS is to let non-technical users create and edit data, and you don’t know what they’re going to do. They might accidentally delete a record, then re-create it and it will be in a different order in the database even if the properties are the same as before. You don’t want your website to break if this happens.

If you do need a specific record from the DB for some reason, it would be better to use ->byID($id) instead of ->first() or ->last(), that way you will get the same record every time*, no matter what order it appears in the list.

*As long as it exists of course :slight_smile:

Hi JonoM,
Yes, exactly as I assume the worst case - that a user could delete it - I didn’t want to solve it with an ID as this will change when a user accidentally deletes it and creates a new one.
In my case I could have added the one additional field I needed for the similar page to the original CustomPage, but I preferred to extend this CustomPage, so I am able to distinct between them and don’t have to worry about the users’ action. As both get only used once on the website I solved this with the following code in the public function UpcomingCustomPageExclusionDays()

public function UpcomingCustomPageExclusionDays(){
        $CustomPageData = CustomPage::get();
        $CustomPageExtendedPageData = CustomPageExtendedPage::get()->First();
        foreach($CustomPageData as $CustomPageDat){
            if ($CustomPageDat->ID != $CustomPageExtendedPageData->ID) {
                $upcomingExclusionDays = $CustomPageData->ThisTermUpcomingExclusion($CustomPageDat);
                return $upcomingExclusionDays;