ModelInterface
API reference › Model › ModelInterface
Implemented by 2 types
Interface implemented by classes supporting model data binding.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
add() abstract | Adds a new property to the model. | ModelInterface |
getData() abstract | Returns the raw data for the model. | ModelInterface |
getModelName() abstract | Returns the model name that this model class should use. | ModelInterface |
getNames() abstract | Returns the list of property names. | ModelInterface |
getTypes() abstract | Returns the list of property types indexed by property names. | ModelInterface |
getValue() abstract | Returns the value (raw data) for the specified property. | ModelInterface |
has() abstract | Checks if the model has the specified property. | ModelInterface |
isEmpty() abstract | Whether the model has no loaded raw data. | ModelInterface |
isType() abstract | Checks if the property is of the specified type. | ModelInterface |
load() abstract | Populates the model with input data. | ModelInterface |
setValue() abstract | Sets the value for the specified property. | ModelInterface |
setValues() abstract | Sets values for multiple properties. | ModelInterface |
toArray() abstract | Returns model properties as an array. | ModelInterface |
Method details
Section titled “Method details”add()
Section titled “add() ”abstract public function add(string $property, array|string $type): voidAdds a new property to the model.
Parameters
| Parameter | Type | Description |
|---|---|---|
$property | string | Property name. |
$type | array| | Property type. This can be a string or an array of strings representing the property type. If an array is given, the property is considered to be multi-valued and the property value should be an array of values of the given types. |
getData()
Section titled “getData() ”abstract public function getData(): arrayReturns the raw data for the model.
Returns
array: Raw data for the model.
getModelName()
Section titled “getModelName() ”abstract public function getModelName(): stringReturns the model name that this model class should use.
The model name is mainly used by BaseModel to decide how to name the input fields for the properties
in a model.
- If the model name is “A” and an property name is “b”, then the corresponding input name would be “A[b]”.
- If the model name is an empty string, then the input name would be “b”.
The purpose of the above naming schema is that for forms which contain multiple different models, the properties of each model are grouped in sub-arrays of the POST-data, and it’s easier to differentiate between them.
- By default, this method returns the model class name (without the namespace part) as the model name.
- You may override it when the model is used in different forms.
Returns
string: Model name class, without a namespace part or empty string when class is anonymous. load()
getNames()
Section titled “getNames() ”abstract public function getNames(): list<string>Returns the list of property names.
Returns
list<string>: List of property names.
getTypes()
Section titled “getTypes() ”abstract public function getTypes(): arrayReturns the list of property types indexed by property names.
Returns
array: List of property types indexed by property names.
getValue()
Section titled “getValue() ”abstract public function getValue(string $property): mixedReturns the value (raw data) for the specified property.
Parameters
| Parameter | Type | Description |
|---|---|---|
$property | string | Property name. |
Returns
mixed: Value (raw data) for the specified property.
has()
Section titled “has() ”abstract public function has(string $property): boolChecks if the model has the specified property.
Parameters
| Parameter | Type | Description |
|---|---|---|
$property | string | Property name. |
Returns
bool: true if the model has the specified property, false otherwise.
isEmpty()
Section titled “isEmpty() ”abstract public function isEmpty(): boolWhether the model has no loaded raw data.
Returns
bool: true if the model has no loaded raw data, false otherwise.
isType()
Section titled “isType() ”abstract public function isType(string $property, string $type): boolChecks if the property is of the specified type.
Parameters
| Parameter | Type | Description |
|---|---|---|
$property | string | Property name. |
$type | string | Type name. |
Returns
bool: true if the property is of the specified type, false otherwise.
load()
Section titled “load() ”abstract public function load(iterable $data, string|null $modelName = null): boolPopulates the model with input data.
The load() method gets the model name from the getModelName() method (which you may override), unless
the $modelName parameter is given.
If the model name is an empty string, load() populates the model with the whole $data array instead of
$data['ModelName'].
Parameters
| Parameter | Type | Description |
|---|---|---|
$data | iterable | Data array to load, typically server request properties. |
$modelName | string| | Scope from which to get data. |
Returns
bool: true if the model is successfully populated with some data, false otherwise.
setValue()
Section titled “setValue() ”abstract public function setValue(string $property, mixed $value): voidSets the value for the specified property.
Parameters
| Parameter | Type | Description |
|---|---|---|
$property | string | Property name. |
$value | mixed | Value to set. |
setValues()
Section titled “setValues() ”abstract public function setValues(array $data, array $except = []): voidSets values for multiple properties.
Parameters
| Parameter | Type | Description |
|---|---|---|
$data | array | Key value pairs to set for the properties. |
$except | array | Properties to exclude from the setting using camelCase names. If not empty, the listed properties are skipped. If empty, all properties from $data are applied. |
toArray()
Section titled “toArray() ”abstract public function toArray(bool $snakeCase = false, array $exceptProperties = []): array<string, mixed>Returns model properties as an array.
Parameters
| Parameter | Type | Description |
|---|---|---|
$snakeCase | bool | Whether keys should be converted to snake_case. |
$exceptProperties | array | List of properties to exclude. |
Returns
array<string, mixed>