Working with a Model
This page considers how to manage a controller model, i.e. its linking and method request. The model’s inner structure is considered in the corresponding section.
Linking a Custom Model
The component automatically detects if there is a custom model when requesting a controller. Once detected, the following object is created:
$this->model
This object is automatically created when launching a controller and is available from any of its methods as well as from its actions located in separate files.
An example of using a model inside a controller:
$latest_posts = $this->model->orderBy('date_pub', 'desc')->limit(10)->getPosts();
Linking a Custom Model
A controller can also use other component’s model.
A static cmsCore::getModel() core method is used to receive custom model’s instance. The title of a component, the model of which is to be received, is passed to it as an argument.
For example, to get a model of the users component:
// We get the model of the users component $users_model = cmsCore::getModel('users'); // We get data from the model $users = $users_model->getUsers();
Back to the "Controllers" section table of contents
Back to the "Models" section table of contents