ModelAwareTrait
Provides functionality for loading table classes and other repositories onto properties of the host object.
Use Cake\ORM\Locator\LocatorAwareTrait
instead.
Example users of this trait are Cake\Controller\Controller and Cake\Console\Shell.
Table of Contents
Properties
- $_modelFactories : array<string|int, callable|LocatorInterface>
- A list of overridden model factory functions.
- $_modelType : string
- The model type to use.
- $modelClass : string|null
- This object's primary model class name. Should be a plural form.
Methods
- getModelType() : string
- Get the model type to be used by this class
- loadModel() : RepositoryInterface
- Loads and constructs repository objects required by this object
- modelFactory() : void
- Override a existing callable to generate repositories of a given type.
- setModelType() : $this
- Set the model type to be used by this class
- _setModelClass() : void
- Set the modelClass property based on conventions.
Properties
$_modelFactories
A list of overridden model factory functions.
protected
array<string|int, callable|LocatorInterface>
$_modelFactories
= []
$_modelType
The model type to use.
protected
string
$_modelType
= 'Table'
$modelClass
This object's primary model class name. Should be a plural form.
Use Cake\ORM\Locator\LocatorAwareTrait::$defaultTable
instead.
protected
string|null
$modelClass
CakePHP will not inflect the name.
Example: For an object named 'Comments', the modelClass would be 'Comments'.
Plugin classes should use Plugin.Comments
style names to correctly load
models from the correct plugin.
Use empty string to not use auto-loading on this object. Null auto-detects based on controller name.
Methods
getModelType()
Get the model type to be used by this class
public
getModelType() : string
Return values
stringloadModel()
Loads and constructs repository objects required by this object
public
loadModel([string|null $modelClass = null ][, string|null $modelType = null ]) : RepositoryInterface
Use LocatorAwareTrait::fetchTable()
instead.
Typically used to load ORM Table objects as required. Can also be used to load other types of repository objects your application uses.
If a repository provider does not return an object a MissingModelException will be thrown.
Parameters
- $modelClass : string|null = null
-
Name of model class to load. Defaults to $this->modelClass. The name can be an alias like
'Post'
or FQCN likeApp\Model\Table\PostsTable::class
. - $modelType : string|null = null
-
The type of repository to load. Defaults to the getModelType() value.
Tags
Return values
RepositoryInterface —The model instance created.
modelFactory()
Override a existing callable to generate repositories of a given type.
public
modelFactory(string $type, LocatorInterface|callable $factory) : void
Parameters
- $type : string
-
The name of the repository type the factory function is for.
- $factory : LocatorInterface|callable
-
The factory function used to create instances.
setModelType()
Set the model type to be used by this class
public
setModelType(string $modelType) : $this
Parameters
- $modelType : string
-
The model type
Return values
$this_setModelClass()
Set the modelClass property based on conventions.
protected
_setModelClass(string $name) : void
If the property is already set it will not be overwritten
Parameters
- $name : string
-
Class name.