Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions en/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -434,22 +434,8 @@ By using the second parameter you can define a status code for your redirect::
See the :ref:`redirect-component-events` section for how to redirect out of
a life-cycle handler.

Loading Additional Models
=========================

.. php:method:: fetchModel(string $alias, array $config = [])

The ``fetchModel()`` method is useful to load models or ORM tables that
are not the controller's default. Models retrieved with this method will not be
set as properties on your controller::

// Get an ElasticSearch model
$articles = $this->fetchModel('Articles', 'Elastic');

// Get a webservices model
$github = $this->fetchModel('GitHub', 'Webservice');

.. versionadded:: 4.5.0
Loading Additional Tables/Models
================================

.. php:method:: fetchTable(string $alias, array $config = [])

Expand All @@ -463,6 +449,25 @@ the controller's default one::
)
->all();

.. php:method:: fetchModel(string|null $modelClass = null, string|null $modelType = null)

The ``fetchModel()`` method is useful to load non ORM models or ORM tables that
are not the controller's default::
Copy link
Member

Choose a reason for hiding this comment

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

or ORM tables that are not the controller's default

This is the same definition as fetchTable() which is confusing. We should describe fetchModel() for just one use case.

Copy link
Member Author

@ADmad ADmad Jan 18, 2024

Choose a reason for hiding this comment

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

Well "non ORM models" is the different bit here. This method can fetch any type of model including tables, which is what you get if you don't specify the 2nd argument.


// ModelAwareTrait need to be explicity added to your controler first for fetchModel() to work.
use ModelAwareTrait;

// Get an ElasticSearch model
$articles = $this->fetchModel('Articles', 'Elastic');

// Get a webservices model
$github = $this->fetchModel('GitHub', 'Webservice');

// If you skip the 2nd argument it will by default try to load a ORM table.
$authors = $this->fetchModel('Authors');

.. versionadded:: 4.5.0

Paginating a Model
==================

Expand Down