Document static getName() for registering a custom Model (Mautic 8) - #630
Document static getName() for registering a custom Model (Mautic 8)#630promptless-for-oss wants to merge 3 commits into
Conversation
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
|
|
||
| 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 |
There was a problem hiding this comment.
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.
|
|
||
| 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:: |
There was a problem hiding this comment.
Core LeadModel declares public static function getName(): string { return 'lead.lead'; }, confirming the doc's example of core's registration pattern.
|
|
||
| ``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: |
There was a problem hiding this comment.
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.
|
|
||
| .. _getting model objects: | ||
|
|
||
| Getting model objects |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
@promptless-for-oss please address the suggestions.
|
|
||
| .. 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. |
There was a problem hiding this comment.
@promptless-for-oss, is there any reason not to add space before and after the hyphen as suggestion below?
| ``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. |
|
|
||
| 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. |
There was a problem hiding this comment.
| 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. |
| 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. |
There was a problem hiding this comment.
| 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. |
|
Thanks @adiati98 — on it. I'll apply your suggested edits on |
|
Update: applying your two sentence-split suggestions on |
Apply maintainer suggestion blocks on docs/plugins/mvc.rst: - Line 402: colon -> period, capitalize 'You still' - Line 415: colon -> period, capitalize 'There's'
|
I applied both of your suggested edits to On your hyphen question for line 419: I kept |
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 itsbundle.namekey, andgetModel('yourbundle.yourmodel')resolves it from that key. This replaces the removedmautic.modelauto-tag, themautic.<bundle>.model.<name>service-alias convention, and theModelPasscompiler 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 thegetName()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.rstline 402 — applied verbatim: split the run-on after "registration" (colon → period, "You still" capitalized).docs/plugins/mvc.rstline 415 — applied verbatim: split the run-on after "step" (colon → period, "There's" capitalized).docs/plugins/mvc.rstline 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 togetName() - basedwould break the attributive compound and read as the mid-sentence dash-aside form the repo reserves for asides (e.g. "same pattern - itsLeadModel"), so it's kept unspaced. Happy to change it if you'd prefer consistency with a different convention.Vale passes on the changed file.