Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Model::findById() instead of Model::findByPk() #6916

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions calendar-bundle/contao/classes/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Calendar extends Frontend
*/
public function generateFeed($intId)
{
$objCalendar = CalendarFeedModel::findByPk($intId);
$objCalendar = CalendarFeedModel::findById($intId);

if ($objCalendar === null)
{
Expand Down Expand Up @@ -158,7 +158,7 @@ protected function generateFiles($arrFeed)
}

// No jumpTo page set (see #4784)
if (!$jumpTo = CalendarModel::findByPk($objArticle->pid)?->jumpTo)
if (!$jumpTo = CalendarModel::findById($objArticle->pid)?->jumpTo)
{
continue;
}
Expand Down Expand Up @@ -231,7 +231,7 @@ protected function generateFiles($arrFeed)
}

// Override the global page object (#2946)
$GLOBALS['objPage'] = $this->getPageWithDetails(CalendarModel::findByPk($event['pid'])->jumpTo);
$GLOBALS['objPage'] = $this->getPageWithDetails(CalendarModel::findById($event['pid'])->jumpTo);

// Override the assets and files context (#6563)
$GLOBALS['objPage']->staticFiles = rtrim($GLOBALS['objPage']->staticFiles ?: $strLink, '/');
Expand All @@ -254,7 +254,7 @@ protected function generateFiles($arrFeed)
$objItem->guid = $event['link'] . '#' . date('Y-m-d', $event['startTime']);
}

if ($objAuthor = UserModel::findByPk($event['author']))
if ($objAuthor = UserModel::findById($event['author']))
{
$objItem->author = $objAuthor->name;
}
Expand Down Expand Up @@ -512,12 +512,12 @@ private function getPageWithDetails($intPageId)
return null;
}

if (!$objLayout = LayoutModel::findByPk($objPage->layout))
if (!$objLayout = LayoutModel::findById($objPage->layout))
{
return $objPage;
}

if (!$objTheme = ThemeModel::findByPk($objLayout->pid))
if (!$objTheme = ThemeModel::findById($objLayout->pid))
{
return $objPage;
}
Expand Down
2 changes: 1 addition & 1 deletion calendar-bundle/contao/classes/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ protected function addEvent($objEvents, $intStart, $intEnd, $intBegin, $intLimit
$arrEvent['day'] = $strDay;
$arrEvent['month'] = $strMonth;
$arrEvent['parent'] = $intCalendar;
$arrEvent['calendar'] = CalendarModel::findByPk($objEvents->pid);
$arrEvent['calendar'] = CalendarModel::findById($objEvents->pid);
$arrEvent['link'] = $objEvents->title;
$arrEvent['target'] = '';
$arrEvent['title'] = StringUtil::specialchars($objEvents->title, true);
Expand Down
10 changes: 5 additions & 5 deletions calendar-bundle/contao/dca/tl_calendar_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public function generateAlias($varValue, DataContainer $dc)
// Generate the alias if there is none
if (!$varValue)
{
$varValue = System::getContainer()->get('contao.slug')->generate($dc->activeRecord->title, CalendarModel::findByPk($dc->activeRecord->pid)->jumpTo, $aliasExists);
$varValue = System::getContainer()->get('contao.slug')->generate($dc->activeRecord->title, CalendarModel::findById($dc->activeRecord->pid)->jumpTo, $aliasExists);
}
elseif (preg_match('/^[1-9]\d*$/', $varValue))
{
Expand Down Expand Up @@ -590,19 +590,19 @@ public function setEmptyEndTime($varValue, DataContainer $dc)
*/
public function getTitleTag(CalendarEventsModel $model)
{
if (!$calendar = CalendarModel::findByPk($model->pid))
if (!$calendar = CalendarModel::findById($model->pid))
{
return '';
}

if (!$page = PageModel::findByPk($calendar->jumpTo))
if (!$page = PageModel::findById($calendar->jumpTo))
{
return '';
}

$page->loadDetails();

if (!$layout = LayoutModel::findByPk($page->layout))
if (!$layout = LayoutModel::findById($page->layout))
{
return '';
}
Expand Down Expand Up @@ -844,7 +844,7 @@ public function scheduleUpdate(DataContainer $dc)
*/
public function addSitemapCacheInvalidationTag($dc, array $tags)
{
$calendar = CalendarModel::findByPk($dc->activeRecord->pid);
$calendar = CalendarModel::findById($dc->activeRecord->pid);

if ($calendar === null)
{
Expand Down
2 changes: 1 addition & 1 deletion calendar-bundle/contao/modules/ModuleCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function generate()
$this->strUrl = preg_replace('/\?.*$/', '', Environment::get('requestUri'));
$this->strLink = $this->strUrl;

if ($objTarget = PageModel::findByPk($this->objModel->jumpTo))
if ($objTarget = PageModel::findById($this->objModel->jumpTo))
{
try
{
Expand Down
8 changes: 4 additions & 4 deletions calendar-bundle/contao/modules/ModuleEventReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function compile()

$urlGenerator = System::getContainer()->get('contao.routing.content_url_generator');

if ($this->overviewPage && ($overviewPage = PageModel::findByPk($this->overviewPage)))
if ($this->overviewPage && ($overviewPage = PageModel::findById($this->overviewPage)))
{
$this->Template->referer = $urlGenerator->generate($overviewPage);
$this->Template->back = $this->customLabel ?: $GLOBALS['TL_LANG']['MSC']['eventOverview'];
Expand Down Expand Up @@ -247,7 +247,7 @@ protected function compile()
$objTemplate->recurring = $recurring;
$objTemplate->until = $until;
$objTemplate->locationLabel = $GLOBALS['TL_LANG']['MSC']['location'];
$objTemplate->calendar = CalendarModel::findByPk($objEvent->pid);
$objTemplate->calendar = CalendarModel::findById($objEvent->pid);
$objTemplate->count = 0; // see #74
$objTemplate->details = '';
$objTemplate->hasTeaser = false;
Expand Down Expand Up @@ -436,7 +436,7 @@ protected function compile()
return;
}

if (!$objCalendar = CalendarModel::findByPk($objEvent->pid))
if (!$objCalendar = CalendarModel::findById($objEvent->pid))
{
return;
}
Expand All @@ -461,7 +461,7 @@ protected function compile()
$arrNotifies[] = $GLOBALS['TL_ADMIN_EMAIL'];
}

if ($objCalendar->notify != 'notify_admin' && ($objAuthor = UserModel::findByPk($objEvent->author)) && $objAuthor->email)
if ($objCalendar->notify != 'notify_admin' && ($objAuthor = UserModel::findById($objEvent->author)) && $objAuthor->email)
{
$arrNotifies[] = $objAuthor->email;
}
Expand Down
2 changes: 1 addition & 1 deletion calendar-bundle/contao/modules/ModuleEventlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ protected function compile()
// Add an image
if ($event['addImage'])
{
$eventModel = CalendarEventsModel::findByPk($event['id']);
$eventModel = CalendarEventsModel::findById($event['id']);
$imgSize = $eventModel->size ?: null;

// Override the default image size
Expand Down
2 changes: 1 addition & 1 deletion calendar-bundle/src/EventListener/InsertTagsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private function replaceCalendarFeedInsertTag(string $feedId): string

$adapter = $this->framework->getAdapter(CalendarFeedModel::class);

if (!$feed = $adapter->findByPk($feedId)) {
if (!$feed = $adapter->findById($feedId)) {
return '';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ private function getEventModel(Request $request): CalendarEventsModel|null
return null;
}

return $this->framework->getAdapter(CalendarEventsModel::class)->findByPk($request->query->get('calendar'));
return $this->framework->getAdapter(CalendarEventsModel::class)->findById($request->query->get('calendar'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ private function getId(PreviewUrlCreateEvent $event, Request $request): int|stri

private function getEventModel(int|string $id): CalendarEventsModel|null
{
return $this->framework->getAdapter(CalendarEventsModel::class)->findByPk($id);
return $this->framework->getAdapter(CalendarEventsModel::class)->findById($id);
}
}
4 changes: 2 additions & 2 deletions calendar-bundle/src/Picker/EventPickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ private function getCalendarId(int|string $id): int|null
{
$eventAdapter = $this->framework->getAdapter(CalendarEventsModel::class);

if (!$eventsModel = $eventAdapter->findByPk($id)) {
if (!$eventsModel = $eventAdapter->findById($id)) {
return null;
}

if (!$calendar = $this->framework->getAdapter(CalendarModel::class)->findByPk($eventsModel->pid)) {
if (!$calendar = $this->framework->getAdapter(CalendarModel::class)->findById($eventsModel->pid)) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion calendar-bundle/src/Routing/CalendarEventsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function resolve(object $content): ContentUrlResult|null
$calendarAdapter = $this->framework->getAdapter(CalendarModel::class);

// Link to the default page
return ContentUrlResult::resolve($pageAdapter->findPublishedById((int) $calendarAdapter->findByPk($content->pid)?->jumpTo));
return ContentUrlResult::resolve($pageAdapter->findPublishedById((int) $calendarAdapter->findById($content->pid)?->jumpTo));
}

public function getParametersForContent(object $content, PageModel $pageModel): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testReplacesTheCalendarFeedTag(): void
$feedModel->alias = 'events';

$adapters = [
CalendarFeedModel::class => $this->mockConfiguredAdapter(['findByPk' => $feedModel]),
CalendarFeedModel::class => $this->mockConfiguredAdapter(['findById' => $feedModel]),
];

$framework = $this->mockContaoFramework($adapters);
Expand Down Expand Up @@ -155,7 +155,7 @@ public function testReturnsAnEmptyStringIfThereIsNoModel(): void
{
$adapters = [
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findByIdOrAlias' => null]),
CalendarFeedModel::class => $this->mockConfiguredAdapter(['findByPk' => null]),
CalendarFeedModel::class => $this->mockConfiguredAdapter(['findById' => null]),
];

$urlGenerator = $this->createMock(ContentUrlGenerator::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testConvertsThePreviewUrl(): void
$eventModel = $this->createMock(CalendarEventsModel::class);

$adapters = [
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findByPk' => $eventModel]),
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findById' => $eventModel]),
];

$framework = $this->mockContaoFramework($adapters);
Expand Down Expand Up @@ -106,7 +106,7 @@ public function testDoesNotConvertThePreviewUrlIfThereIsNoEvent(): void
$request->server->set('SERVER_PORT', 80);

$adapters = [
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findByPk' => null]),
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findById' => null]),
];

$framework = $this->mockContaoFramework($adapters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testCreatesThePreviewUrl(): void
$eventModel->id = 1;

$adapters = [
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findByPk' => $eventModel]),
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findById' => $eventModel]),
];

$framework = $this->mockContaoFramework($adapters);
Expand Down Expand Up @@ -101,7 +101,7 @@ public function testOverwritesTheIdIfTheEventSettingsAreEdited(): void
$eventModel->id = 2;

$adapters = [
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findByPk' => $eventModel]),
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findById' => $eventModel]),
];

$framework = $this->mockContaoFramework($adapters);
Expand All @@ -119,7 +119,7 @@ public function testDoesNotCreateThePreviewUrlIfThereIsNoEvent(): void
$requestStack->push(new Request());

$adapters = [
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findByPk' => null]),
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findById' => null]),
];

$framework = $this->mockContaoFramework($adapters);
Expand Down
10 changes: 5 additions & 5 deletions calendar-bundle/tests/Picker/EventPickerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public function testAddsTableAndIdIfThereIsAValue(): void
$config = new PickerConfig('link', [], '{{event_url::1}}', 'eventPicker');

$adapters = [
CalendarModel::class => $this->mockConfiguredAdapter(['findByPk' => $calendarModel]),
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findByPk' => $calendarEvents]),
CalendarModel::class => $this->mockConfiguredAdapter(['findById' => $calendarModel]),
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findById' => $calendarEvents]),
];

$picker = $this->getPicker();
Expand All @@ -165,7 +165,7 @@ public function testDoesNotAddTableAndIdIfThereIsNoEventsModel(): void
$config = new PickerConfig('link', [], '{{event_url::1}}', 'eventPicker');

$adapters = [
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findByPk' => null]),
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findById' => null]),
];

$picker = $this->getPicker();
Expand All @@ -185,8 +185,8 @@ public function testDoesNotAddTableAndIdIfThereIsNoCalendarModel(): void
$config = new PickerConfig('link', [], '{{event_url::1}}', 'eventPicker');

$adapters = [
CalendarModel::class => $this->mockConfiguredAdapter(['findByPk' => null]),
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findByPk' => $calendarEvents]),
CalendarModel::class => $this->mockConfiguredAdapter(['findById' => null]),
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findById' => $calendarEvents]),
];

$picker = $this->getPicker();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function testResolveEventWithoutSource(): void

$framework = $this->mockContaoFramework([
PageModel::class => $pageAdapter,
CalendarModel::class => $this->mockConfiguredAdapter(['findByPk' => $calendar]),
CalendarModel::class => $this->mockConfiguredAdapter(['findById' => $calendar]),
]);

$resolver = new CalendarEventsResolver($framework);
Expand Down
4 changes: 2 additions & 2 deletions comments-bundle/contao/classes/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function addCommentsToTemplate(FrontendTemplate $objTemplate, \stdClass $
$objPartial->addReply = false;

// Reply
if ($objComments->addReply && $objComments->reply && ($objAuthor = UserModel::findByPk($objComments->author)))
if ($objComments->addReply && $objComments->reply && ($objAuthor = UserModel::findById($objComments->author)))
{
$objPartial->addReply = true;
$objPartial->rby = $GLOBALS['TL_LANG']['MSC']['com_reply'];
Expand Down Expand Up @@ -587,7 +587,7 @@ public static function changeSubscriptionStatus(FrontendTemplate $objTemplate)
$optIn = System::getContainer()->get('contao.opt_in');

// Find an unconfirmed token with only one related record
if ((!$optInToken = $optIn->find(Input::get('token'))) || !$optInToken->isValid() || \count($arrRelated = $optInToken->getRelatedRecords()) != 1 || key($arrRelated) != 'tl_comments_notify' || \count($arrIds = current($arrRelated)) != 1 || (!$objNotify = CommentsNotifyModel::findByPk($arrIds[0])))
if ((!$optInToken = $optIn->find(Input::get('token'))) || !$optInToken->isValid() || \count($arrRelated = $optInToken->getRelatedRecords()) != 1 || key($arrRelated) != 'tl_comments_notify' || \count($arrIds = current($arrRelated)) != 1 || (!$objNotify = CommentsNotifyModel::findById($arrIds[0])))
{
$objTemplate->confirm = $GLOBALS['TL_LANG']['MSC']['invalidToken'];

Expand Down
4 changes: 2 additions & 2 deletions comments-bundle/contao/dca/tl_comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public function sendNotifications($varValue)
{
if ($varValue && ($id = Input::get('id')))
{
Comments::notifyCommentsSubscribers(CommentsModel::findByPk($id));
Comments::notifyCommentsSubscribers(CommentsModel::findById($id));
}

return $varValue;
Expand Down Expand Up @@ -646,7 +646,7 @@ public function toggleIcon($row, $href, $label, $title, $icon, $attributes)
*/
public function invalidateSourceCacheTags(DataContainer $dc, array $tags)
{
$commentModel = CommentsModel::findByPk($dc->id);
$commentModel = CommentsModel::findById($dc->id);

if (null !== $commentModel)
{
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/contao/classes/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function getRootPageFromUrl()
{
$objPage->loadDetails();

return PageModel::findByPk($objPage->rootId);
return PageModel::findById($objPage->rootId);
}
}

Expand Down
2 changes: 1 addition & 1 deletion core-bundle/contao/classes/Hybrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function __construct($objElement, $strColumn='main')
// Load the model
if (class_exists($strModelClass))
{
$objHybrid = $strModelClass::findByPk($objElement->{$this->strKey});
$objHybrid = $strModelClass::findById($objElement->{$this->strKey});

if ($objHybrid === null)
{
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/contao/classes/Versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ public static function addToTemplate(BackendTemplate $objTemplate)
if (isset($arrRow['editUrl']))
{
// Adjust the edit URL of files in case they have been renamed (see #671)
if ($arrRow['fromTable'] == 'tl_files' && ($filesModel = FilesModel::findByPk($arrRow['pid'])))
if ($arrRow['fromTable'] == 'tl_files' && ($filesModel = FilesModel::findById($arrRow['pid'])))
{
$arrRow['editUrl'] = preg_replace('/id=[^&]+/', 'id=' . $filesModel->path, $arrRow['editUrl']);
}
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/contao/controllers/BackendPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function run()
}
}

$objUser = UserModel::findByPk($user->id);
$objUser = UserModel::findById($user->id);
$objUser->pwChange = false;
$objUser->password = $passwordHasher->hash($pw);
$objUser->save();
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/contao/dca/tl_article.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function getActiveLayoutSections(DataContainer $dc)
// Get the layout sections
if ($objPage->layout)
{
$objLayout = LayoutModel::findByPk($objPage->layout);
$objLayout = LayoutModel::findById($objPage->layout);

if ($objLayout === null)
{
Expand Down Expand Up @@ -449,7 +449,7 @@ public function addAliasButton($arrButtons, DataContainer $dc)

foreach ($ids as $id)
{
$objArticle = ArticleModel::findByPk($id);
$objArticle = ArticleModel::findById($id);

if ($objArticle === null)
{
Expand Down