Skip to content

Document static getName() for registering a custom Model (Mautic 8) - #630

Open
promptless-for-oss wants to merge 3 commits into
mautic:7.2from
Promptless:promptless/pr-17133-model-getname
Open

Document static getName() for registering a custom Model (Mautic 8)#630
promptless-for-oss wants to merge 3 commits into
mautic:7.2from
Promptless:promptless/pr-17133-model-getname

Conversation

@promptless-for-oss

@promptless-for-oss promptless-for-oss commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Open in Promptless

Mautic 8 changes how a plugin's custom Model is registered for key-based lookup. A Model now declares a static getName() method returning its bundle.name key, and getModel('yourbundle.yourmodel') resolves it from that key. This replaces the removed mautic.model auto-tag, the mautic.<bundle>.model.<name> service-alias convention, and the ModelPass compiler pass; getModel() no longer accepts a fully-qualified class name (inject or type-hint the concrete class to fetch a Model that way).

Adds a "Registering a model" subsection to the plugins MVC page (docs/plugins/mvc.rst) covering the getName() requirement, a code example, the Mautic 8 version note, and the class-name lookup change, cross-referencing the existing "Getting model objects" section.

Source: mautic/mautic PR #17133.

Trigger Events


Review feedback — @adiati98 (review)

  • docs/plugins/mvc.rst line 402 — applied verbatim: split the run-on after "registration" (colon → period, "You still" capitalized).
  • docs/plugins/mvc.rst line 415 — applied verbatim: split the run-on after "step" (colon → period, "There's" capitalized).
  • docs/plugins/mvc.rst line 419 (spaced-hyphen question) — declined, with grounds: getName()-based is a compound adjective modifying "resolution", parallel to this repo's own unspaced compounds — "key-based lookup" (same section), "AJAX-driven", "JavaScript-driven". Spacing it to getName() - based would break the attributive compound and read as the mid-sentence dash-aside form the repo reserves for asides (e.g. "same pattern - its LeadModel"), so it's kept unspaced. Happy to change it if you'd prefer consistency with a different convention.

Vale passes on the changed file.

Mautic 8 replaces the ModelPass compiler pass with an AutowireLocator
keyed by each Model's static getName(). Add a 'Registering a model'
subsection to the plugins MVC page explaining that a custom Model
declares getName() to be resolvable via getModel(), that this replaces
the removed mautic.model tag / service-alias / ModelPass wiring, and
that getModel() no longer accepts a class name.

Source: mautic/mautic PR #17133
Comment thread docs/plugins/mvc.rst

Add the method to a Model class that extends ``AbstractCommonModel`` or ``FormModel``. For example, a ``ContactModel`` built on one of those base classes returns ``'helloworld.contact'``:

.. code-block:: php

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ModelFactory::getModel() resolves models via AutowireLocator(MauticModelInterface::class, defaultIndexMethod: 'getName'); each model must declare static getName() returning the bundle.name key that getModel() looks up in the locator.

Source: https://github.com/mautic/mautic/blob/a0289c20ad552f81488327e2f93f18c134edb2b7/app/bundles/CoreBundle/Factory/ModelFactory.php#L16-L42

Comment thread docs/plugins/mvc.rst

Declaring ``getName()`` is the whole registration step: there's no separate tag, service alias, or compiler-pass step to add. If a Model omits ``getName()``, ``getModel()`` can't resolve it by key.

.. note::

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core LeadModel declares public static function getName(): string { return 'lead.lead'; }, confirming the doc's example of core's registration pattern.

Source: https://github.com/mautic/mautic/blob/a0289c20ad552f81488327e2f93f18c134edb2b7/app/bundles/LeadBundle/Model/LeadModel.php#L102-L105

Comment thread docs/plugins/mvc.rst

``getModel()`` accepts only the ``getName()`` key, not a fully qualified class name. Fetching a Model by its class means injecting or type-hinting the concrete class instead, as described in :ref:`Getting model objects <getting model objects>`.

.. _getting model objects:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UPGRADE-8.0.md confirms getName()-based resolution replaces the removed ModelPass compiler pass and the mautic..model. service aliases (see also line 165), and that ModelFactory::getModel() no longer accepts a fully-qualified class name.

Source: https://github.com/mautic/mautic/blob/a0289c20ad552f81488327e2f93f18c134edb2b7/UPGRADE-8.0.md#L176-L185

Comment thread docs/plugins/mvc.rst

.. _getting model objects:

Getting model objects

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR 17133 diff to app/bundles/CoreBundle/Factory/ModelFactory.php removes the prior class_exists($modelNameKey) && $this->container->has($modelNameKey) fully-qualified-class-name branch from getModel(), confirming getModel() no longer accepts an FQCN.

Source: mautic/mautic@a0289c2

@adiati98 adiati98 added this to the 8.0 milestone Sep 2, 2026

@adiati98 adiati98 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@promptless-for-oss please address the suggestions.

Comment thread docs/plugins/mvc.rst

.. note::

``getName()``-based resolution is the Mautic 8 mechanism. It replaces the removed ``mautic.model`` auto-tag, the manual ``mautic.<bundle>.model.<name>`` service-alias convention, and the ``ModelPass`` compiler pass.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@promptless-for-oss, is there any reason not to add space before and after the hyphen as suggestion below?

Suggested change
``getName()``-based resolution is the Mautic 8 mechanism. It replaces the removed ``mautic.model`` auto-tag, the manual ``mautic.<bundle>.model.<name>`` service-alias convention, and the ``ModelPass`` compiler pass.
``getName()`` - based resolution is the Mautic 8 mechanism. It replaces the removed ``mautic.model`` auto-tag, the manual ``mautic.<bundle>.model.<name>`` service-alias convention, and the ``ModelPass`` compiler pass.

Comment thread docs/plugins/mvc.rst Outdated

Mautic core follows the same pattern - its ``LeadModel`` returns ``'lead.lead'``.

Declaring ``getName()`` is the whole registration step: there's no separate tag, service alias, or compiler-pass step to add. If a Model omits ``getName()``, ``getModel()`` can't resolve it by key.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Declaring ``getName()`` is the whole registration step: there's no separate tag, service alias, or compiler-pass step to add. If a Model omits ``getName()``, ``getModel()`` can't resolve it by key.
Declaring ``getName()`` is the whole registration step. There's no separate tag, service alias, or compiler-pass step to add. If a Model omits ``getName()``, ``getModel()`` can't resolve it by key.

Comment thread docs/plugins/mvc.rst Outdated
Registering a model
====================

To make a custom Model resolvable through ``getModel('yourbundle.yourmodel')`` from a Controller, the Model class declares a static ``getName()`` method that returns that key string. The Model must also implement ``Mautic\CoreBundle\Model\MauticModelInterface``. Extending one of the base classes in Base model classes satisfies that interface requirement, but not the registration: you still declare ``getName()`` on the Model to make it resolvable by key. Declaring ``getName()`` only matters for this key-based lookup - a Model you always inject or type-hint by its concrete class, as described in :ref:`Getting model objects <getting model objects>`, doesn't need it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To make a custom Model resolvable through ``getModel('yourbundle.yourmodel')`` from a Controller, the Model class declares a static ``getName()`` method that returns that key string. The Model must also implement ``Mautic\CoreBundle\Model\MauticModelInterface``. Extending one of the base classes in Base model classes satisfies that interface requirement, but not the registration: you still declare ``getName()`` on the Model to make it resolvable by key. Declaring ``getName()`` only matters for this key-based lookup - a Model you always inject or type-hint by its concrete class, as described in :ref:`Getting model objects <getting model objects>`, doesn't need it.
To make a custom Model resolvable through ``getModel('yourbundle.yourmodel')`` from a Controller, the Model class declares a static ``getName()`` method that returns that key string. The Model must also implement ``Mautic\CoreBundle\Model\MauticModelInterface``. Extending one of the base classes in Base model classes satisfies that interface requirement, but not the registration. You still declare ``getName()`` on the Model to make it resolvable by key. Declaring ``getName()`` only matters for this key-based lookup - a Model you always inject or type-hint by its concrete class, as described in :ref:`Getting model objects <getting model objects>`, doesn't need it.

@promptless-for-oss

Copy link
Copy Markdown
Contributor Author

Thanks @adiati98 — on it. I'll apply your suggested edits on docs/plugins/mvc.rst, including the spaced hyphen around getName()-based resolution, run Vale, and push the update to this PR.

@promptless-for-oss

Copy link
Copy Markdown
Contributor Author

Update: applying your two sentence-split suggestions on mvc.rst verbatim. On the getName()-based hyphen I'm double-checking against the repo's convention for compound adjectives — I'll cover that in my reply with the change. Pushing shortly.

Apply maintainer suggestion blocks on docs/plugins/mvc.rst:
- Line 402: colon -> period, capitalize 'You still'
- Line 415: colon -> period, capitalize 'There's'
@promptless-for-oss

Copy link
Copy Markdown
Contributor Author

I applied both of your suggested edits to docs/plugins/mvc.rst exactly as written — the two run-on sentences (after "registration" on line 402 and after "step" on line 415) are now split into separate sentences.

On your hyphen question for line 419: I kept getName()-based unspaced. It's a compound adjective modifying "resolution" — the same construction as this page's own "key-based lookup" and the repo's "AJAX-driven" / "JavaScript-driven", which are all written without spaces. Spacing it to getName() - based would break that compound and read as a mid-sentence dash aside (the form the repo uses for asides like "same pattern - its LeadModel"), so unspaced is both more grammatically correct and more consistent with the surrounding docs — but happy to change it if you'd prefer a different convention. Vale passes on the changed file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants