Skip to content

Commit

Permalink
Explicitly register the model classes (see #1951)
Browse files Browse the repository at this point in the history
Description
-----------

I have realized that using `Model::getRelated()` still relies on the old Contao 3 class autoloader to find the related model class. This is because `Model::getClassFromTable()` only converts `tl_page` to `PageModel` and not to `Contao\PageModel` and therefore we still have "composerized" classes in Contao 4.10 when we really should not anymore. 😄 

<img width="724" alt="" src="https://user-images.githubusercontent.com/1192057/88038330-5c9a8080-cb46-11ea-9976-6441d9ab906a.png">

This PR explicitly registers the fully qualified model class names.

Commits
-------

9857c38 Explicitly register the model classes
3cb436f Append to TL_MODELS instead of overwriting it
c88a355 Trigger a deprecation for unregistered tables
  • Loading branch information
leofeyer committed Jul 21, 2020
1 parent e06d9f6 commit e159caa
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 1 deletion.
8 changes: 8 additions & 0 deletions calendar-bundle/src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/

use Contao\Calendar;
use Contao\CalendarEventsModel;
use Contao\CalendarFeedModel;
use Contao\CalendarModel;
use Contao\ListWizard;
use Contao\ModuleCalendar;
use Contao\ModuleEventlist;
Expand Down Expand Up @@ -52,3 +55,8 @@
$GLOBALS['TL_PERMISSIONS'][] = 'calendarp';
$GLOBALS['TL_PERMISSIONS'][] = 'calendarfeeds';
$GLOBALS['TL_PERMISSIONS'][] = 'calendarfeedp';

// Models
$GLOBALS['TL_MODELS']['tl_calendar_events'] = CalendarEventsModel::class;
$GLOBALS['TL_MODELS']['tl_calendar_feed'] = CalendarFeedModel::class;
$GLOBALS['TL_MODELS']['tl_calendar'] = CalendarModel::class;
6 changes: 6 additions & 0 deletions comments-bundle/src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/

use Contao\Comments;
use Contao\CommentsModel;
use Contao\CommentsNotifyModel;
use Contao\ContentComments;
use Contao\ModuleComments;

Expand All @@ -27,3 +29,7 @@

// Cron jobs
$GLOBALS['TL_CRON']['daily']['purgeCommentSubscriptions'] = array(Comments::class, 'purgeSubscriptions');

// Models
$GLOBALS['TL_MODELS']['tl_comments'] = CommentsModel::class;
$GLOBALS['TL_MODELS']['tl_comments_notify'] = CommentsNotifyModel::class;
41 changes: 40 additions & 1 deletion core-bundle/src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @license LGPL-3.0-or-later
*/

use Contao\ArticleModel;
use Contao\Automator;
use Contao\CheckBox;
use Contao\CheckBoxWizard;
Expand All @@ -28,6 +29,7 @@
use Contao\ContentList;
use Contao\ContentMarkdown;
use Contao\ContentMedia;
use Contao\ContentModel;
use Contao\ContentModule;
use Contao\ContentSliderStart;
use Contao\ContentSliderStop;
Expand All @@ -40,16 +42,19 @@
use Contao\CoreBundle\Controller\BackendCsvImportController;
use Contao\Crawl;
use Contao\FileSelector;
use Contao\FilesModel;
use Contao\FileTree;
use Contao\Form;
use Contao\FormCaptcha;
use Contao\FormCheckBox;
use Contao\FormExplanation;
use Contao\FormFieldModel;
use Contao\FormFieldsetStart;
use Contao\FormFieldsetStop;
use Contao\FormFileUpload;
use Contao\FormHidden;
use Contao\FormHtml;
use Contao\FormModel;
use Contao\FormPassword;
use Contao\FormRadioButton;
use Contao\FormRange;
Expand All @@ -58,10 +63,15 @@
use Contao\FormTextArea;
use Contao\FormTextField;
use Contao\ImageSize;
use Contao\ImageSizeItemModel;
use Contao\ImageSizeModel;
use Contao\InputUnit;
use Contao\KeyValueWizard;
use Contao\LayoutModel;
use Contao\ListWizard;
use Contao\Maintenance;
use Contao\MemberGroupModel;
use Contao\MemberModel;
use Contao\Messages;
use Contao\MetaWizard;
use Contao\ModuleArticleList;
Expand All @@ -75,6 +85,7 @@
use Contao\ModuleLogin;
use Contao\ModuleLogout;
use Contao\ModuleMaintenance;
use Contao\ModuleModel;
use Contao\ModuleNavigation;
use Contao\ModulePassword;
use Contao\ModulePersonalData;
Expand All @@ -87,12 +98,14 @@
use Contao\ModuleSitemap;
use Contao\ModuleTwoFactor;
use Contao\ModuleWizard;
use Contao\OptInModel;
use Contao\OptionWizard;
use Contao\PageError401;
use Contao\PageError403;
use Contao\PageError404;
use Contao\PageForward;
use Contao\PageLogout;
use Contao\PageModel;
use Contao\PageRedirect;
use Contao\PageRegular;
use Contao\PageRoot;
Expand All @@ -107,16 +120,21 @@
use Contao\SelectMenu;
use Contao\SerpPreview;
use Contao\StringUtil;
use Contao\StyleModel;
use Contao\StyleSheetModel;
use Contao\StyleSheets;
use Contao\System;
use Contao\TableWizard;
use Contao\TextArea;
use Contao\TextField;
use Contao\TextStore;
use Contao\Theme;
use Contao\ThemeModel;
use Contao\TimePeriod;
use Contao\TrblField;
use Contao\Upload;
use Contao\UserGroupModel;
use Contao\UserModel;

// Back end modules
$GLOBALS['BE_MOD'] = array
Expand Down Expand Up @@ -537,6 +555,27 @@
'separator' => array()
);

// Models
$GLOBALS['TL_MODELS'] = array(
'tl_article' => ArticleModel::class,
'tl_content' => ContentModel::class,
'tl_files' => FilesModel::class,
'tl_form_field' => FormFieldModel::class,
'tl_form' => FormModel::class,
'tl_image_size_item' => ImageSizeItemModel::class,
'tl_image_size' => ImageSizeModel::class,
'tl_layout' => LayoutModel::class,
'tl_member_group' => MemberGroupModel::class,
'tl_member' => MemberModel::class,
'tl_module' => ModuleModel::class,
'tl_opt_in' => OptInModel::class,
'tl_page' => PageModel::class,
'tl_style' => StyleModel::class,
'tl_style_sheet' => StyleSheetModel::class,
'tl_theme' => ThemeModel::class,
'tl_user_group' => UserGroupModel::class,
'tl_user' => UserModel::class
);

// Other global arrays
$GLOBALS['TL_MODELS'] = array();
$GLOBALS['TL_PERMISSIONS'] = array();
2 changes: 2 additions & 0 deletions core-bundle/src/Resources/contao/library/Contao/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,8 @@ public static function getClassFromTable($strTable)
return static::$arrClassNames[$strTable];
}

trigger_deprecation('contao/core-bundle', '4.10', sprintf('Not registering table "%s" in $GLOBALS[\'TL_MODELS\'] has been deprecated and will no longer work in Contao 5.0.', $strTable));

$arrChunks = explode('_', $strTable);

if ($arrChunks[0] == 'tl')
Expand Down
6 changes: 6 additions & 0 deletions faq-bundle/src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @license LGPL-3.0-or-later
*/

use Contao\FaqCategoryModel;
use Contao\FaqModel;
use Contao\ModuleFaq;
use Contao\ModuleFaqList;
use Contao\ModuleFaqPage;
Expand Down Expand Up @@ -39,3 +41,7 @@
// Add permissions
$GLOBALS['TL_PERMISSIONS'][] = 'faqs';
$GLOBALS['TL_PERMISSIONS'][] = 'faqp';

// Models
$GLOBALS['TL_MODELS']['tl_faq_category'] = FaqCategoryModel::class;
$GLOBALS['TL_MODELS']['tl_faq'] = FaqModel::class;
8 changes: 8 additions & 0 deletions news-bundle/src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Contao\ModuleNewsMenu;
use Contao\ModuleNewsReader;
use Contao\News;
use Contao\NewsArchiveModel;
use Contao\NewsFeedModel;
use Contao\NewsModel;
use Contao\TableWizard;

// Back end modules
Expand Down Expand Up @@ -52,3 +55,8 @@
$GLOBALS['TL_PERMISSIONS'][] = 'newp';
$GLOBALS['TL_PERMISSIONS'][] = 'newsfeeds';
$GLOBALS['TL_PERMISSIONS'][] = 'newsfeedp';

// Models
$GLOBALS['TL_MODELS']['tl_news_archive'] = NewsArchiveModel::class;
$GLOBALS['TL_MODELS']['tl_news_feed'] = NewsFeedModel::class;
$GLOBALS['TL_MODELS']['tl_news'] = NewsModel::class;
10 changes: 10 additions & 0 deletions newsletter-bundle/src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
use Contao\ModuleSubscribe;
use Contao\ModuleUnsubscribe;
use Contao\Newsletter;
use Contao\NewsletterChannelModel;
use Contao\NewsletterDenyListModel;
use Contao\NewsletterModel;
use Contao\NewsletterRecipientsModel;

// Back end modules
$GLOBALS['BE_MOD']['content']['newsletter'] = array
Expand Down Expand Up @@ -44,3 +48,9 @@

// Cron jobs
$GLOBALS['TL_CRON']['daily']['purgeNewsletterSubscriptions'] = array(Newsletter::class, 'purgeSubscriptions');

// Models
$GLOBALS['TL_MODELS']['tl_newsletter_channel'] = NewsletterChannelModel::class;
$GLOBALS['TL_MODELS']['tl_newsletter_deny_list'] = NewsletterDenyListModel::class;
$GLOBALS['TL_MODELS']['tl_newsletter'] = NewsletterModel::class;
$GLOBALS['TL_MODELS']['tl_newsletter_recipients'] = NewsletterRecipientsModel::class;

0 comments on commit e159caa

Please sign in to comment.