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().
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:
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!
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();
Debug::show($CustomPageData);
$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.
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.
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;
}
}
}
```