Skip to content

Commit 35b7f70

Browse files
Merge pull request #311 from creative-commoners/pulls/2.9/broken-builds
MNT Handle DatabaseExceptions thrown by PHP 8.1
2 parents eea7d9e + d14cf42 commit 35b7f70

1 file changed

Lines changed: 45 additions & 14 deletions

File tree

src/PageTypes/DatedUpdateHolder.php

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use DateTime;
66
use Page;
77
use SilverStripe\CMS\Model\SiteTree;
8+
use SilverStripe\Control\Controller;
89
use SilverStripe\Control\Director;
910
use SilverStripe\Control\HTTP;
1011
use SilverStripe\Core\Config\Config;
1112
use SilverStripe\Core\Convert;
1213
use SilverStripe\ORM\ArrayList;
14+
use SilverStripe\ORM\Connect\DatabaseException;
1315
use SilverStripe\ORM\DataList;
1416
use SilverStripe\ORM\DataObject;
1517
use SilverStripe\ORM\PaginatedList;
@@ -146,6 +148,15 @@ public static function AllUpdates(
146148
));
147149
}
148150

151+
try {
152+
// Try running query inside try/catch block to handle any invalid date format
153+
$items->dataQuery()->execute();
154+
} catch (DatabaseException $e) {
155+
self::handleInvalidDateFormat($e);
156+
// Ensure invalid SQL does not get run again
157+
$items = $className::get()->limit(0);
158+
}
159+
149160
// Unpaginated DataList.
150161
return $items;
151162
}
@@ -185,20 +196,25 @@ public static function ExtractMonths(
185196
$link = Director::makeRelative($_SERVER['REQUEST_URI']);
186197
}
187198

188-
$dates = $updates->dataQuery()
189-
->groupby('YEAR("Date")')
190-
->groupby('MONTH("Date")')
191-
->query()
192-
->setSelect([
193-
'Year' => 'YEAR("Date")',
194-
'Month' => 'MONTH("Date")',
195-
])
196-
->addWhere('"Date" IS NOT NULL')
197-
->setOrderBy([
198-
'YEAR("Date")' => 'DESC',
199-
'MONTH("Date")' => 'DESC',
200-
])
201-
->execute();
199+
$dates = [];
200+
try {
201+
$dates = $updates->dataQuery()
202+
->groupby('YEAR("Date")')
203+
->groupby('MONTH("Date")')
204+
->query()
205+
->setSelect([
206+
'Year' => 'YEAR("Date")',
207+
'Month' => 'MONTH("Date")',
208+
])
209+
->addWhere('"Date" IS NOT NULL')
210+
->setOrderBy([
211+
'YEAR("Date")' => 'DESC',
212+
'MONTH("Date")' => 'DESC',
213+
])
214+
->execute();
215+
} catch (DatabaseException $e) {
216+
self::handleInvalidDateFormat($e);
217+
}
202218

203219
$years = [];
204220
foreach ($dates as $date) {
@@ -260,4 +276,19 @@ public function getSubscriptionTitle()
260276
{
261277
return $this->Title;
262278
}
279+
280+
private static function handleInvalidDateFormat(DatabaseException $e): void
281+
{
282+
$controller = Controller::curr();
283+
if ($controller instanceof DatedUpdateHolderController &&
284+
strpos($e->getMessage(), 'Incorrect DATETIME value') !== false
285+
) {
286+
$controller->getRequest()->getSession()->set(DatedUpdateHolderController::TEMP_FORM_MESSAGE, _t(
287+
DatedUpdateHolderController::class . '.InvalidDateFormat',
288+
'Dates must be in "y-MM-dd" format.'
289+
));
290+
} else {
291+
throw $e;
292+
}
293+
}
263294
}

0 commit comments

Comments
 (0)