# DynamicModel

> DynamicModel is a model class that supports defining attributes at run-time (the so-called "dynamic attributes") using its constructor or defineAttribute().

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [base](/docs/api/yii/base/index.md) › DynamicModel

| class | `yii\base\DynamicModel` |
| --- | --- |
| Extends | [`Model`](/docs/api/yii/base/model/index.md) » [`Component`](/docs/api/yii/base/component/index.md) » [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`Configurable`](/docs/api/yii/base/configurable/index.md), [`StaticInstanceInterface`](/docs/api/yii/base/staticinstanceinterface/index.md), [`IteratorAggregate`](https://www.php.net/manual/en/class.iteratoraggregate.php), [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php), [`Arrayable`](/docs/api/yii/base/arrayable/index.md) |
| Uses traits | [`ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md), [`StaticInstanceTrait`](/docs/api/yii/base/staticinstancetrait/index.md) |
| Since | 2.0 |
| Source | [`framework/base/DynamicModel.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/base/DynamicModel.php#L57) |

DynamicModel is a model class that supports defining attributes at run-time (the so-called
"dynamic attributes") using its constructor or [`defineAttribute()`](/docs/api/yii/base/dynamicmodel/index.md#method-defineAttribute). DynamicModel can be used
to support ad hoc data validation.

The typical usage of DynamicModel is as follows,

```
public function actionSearch($name, $email)
{
    $model = DynamicModel::validateData(compact('name', 'email'), [
        [['name', 'email'], 'string', 'max' => 128],
        ['email', 'email'],
    ]);
    if ($model->hasErrors()) {
        // validation fails
    } else {
        // validation succeeds
    }
}
```

The above example shows how to validate `$name` and `$email` with the help of DynamicModel.
The [`validateData()`](/docs/api/yii/base/dynamicmodel/index.md#method-validateData) method creates an instance of DynamicModel, defines the attributes
using the given data (`name` and `email` in this example), and then calls [`Model::validate()`](/docs/api/yii/base/model/index.md#method-validate).

You can check the validation result using [`hasErrors()`](/docs/api/yii/base/model/index.md#method-hasErrors), like you do with a normal model.
You may also access the dynamic attributes defined through the model instance, e.g.,
`$model->name` and `$model->email`.

Alternatively, you may use the following more "classic" syntax to perform ad-hoc data validation:

```
$model = new DynamicModel(compact('name', 'email'));
$model->addRule(['name', 'email'], 'string', ['max' => 128])
    ->addRule('email', 'email')
    ->validate();
```

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-activeValidators"></span>[`$activeValidators`](/docs/api/yii/base/model/index.md#property-activeValidators) read-only | [`yii\validators\Validator`](/docs/api/yii/validators/validator/index.md)`[]` | The validators applicable to the current [`$scenario`](/docs/api/yii/base/model/index.md#property-scenario). | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-attributeLabels"></span>`$attributeLabels` write-only | `string[]` | Attribute labels. | `DynamicModel` |
| <span id="property-attributes"></span>[`$attributes`](/docs/api/yii/base/model/index.md#property-attributes) | `array<string, mixed>` | Attribute values (name => value). | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-errors"></span>[`$errors`](/docs/api/yii/base/model/index.md#property-errors) read-only | `array<string, string[]>` | Errors for all attributes. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-firstErrors"></span>[`$firstErrors`](/docs/api/yii/base/model/index.md#property-firstErrors) read-only | `array<string, string>` | The first errors. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-iterator"></span>[`$iterator`](/docs/api/yii/base/model/index.md#property-iterator) read-only | [`ArrayIterator`](https://www.php.net/manual/en/class.arrayiterator.php)`<string, mixed>` | An iterator for traversing the items in the list. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-scenario"></span>[`$scenario`](/docs/api/yii/base/model/index.md#property-scenario) | `string` | The scenario that this model is in. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-validators"></span>[`$validators`](/docs/api/yii/base/model/index.md#property-validators) read-only | [`ArrayObject`](https://www.php.net/manual/en/class.arrayobject.php)`<int,` [`yii\validators\Validator`](/docs/api/yii/validators/validator/index.md)`>\|`[`yii\validators\Validator`](/docs/api/yii/validators/validator/index.md)`[]` | All the validators declared in the model. | [`Model`](/docs/api/yii/base/model/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Constructor. | `DynamicModel` |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/model/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`Model`](/docs/api/yii/base/model/index.md) |
| [`__get()`](#method-__get) | Returns the value of a component property. | `DynamicModel` |
| [`__isset()`](#method-__isset) | Checks if a property is set, i.e. defined and not null. | `DynamicModel` |
| [`__set()`](#method-__set) | Sets the value of a component property. | `DynamicModel` |
| [`__unset()`](#method-__unset) | Sets a component property to be null. | `DynamicModel` |
| <span id="method-activeAttributes"></span>[`activeAttributes()`](/docs/api/yii/base/model/index.md#method-activeAttributes) | Returns the attribute names that are subject to validation in the current scenario. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-addError"></span>[`addError()`](/docs/api/yii/base/model/index.md#method-addError) | Adds a new error to the specified attribute. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-addErrors"></span>[`addErrors()`](/docs/api/yii/base/model/index.md#method-addErrors) | Adds a list of errors. | [`Model`](/docs/api/yii/base/model/index.md) |
| [`addRule()`](#method-addRule) | Adds a validation rule to this model. | `DynamicModel` |
| <span id="method-afterValidate"></span>[`afterValidate()`](/docs/api/yii/base/model/index.md#method-afterValidate) | This method is invoked after validation ends. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attributeHints"></span>[`attributeHints()`](/docs/api/yii/base/model/index.md#method-attributeHints) | Returns the attribute hints. | [`Model`](/docs/api/yii/base/model/index.md) |
| [`attributeLabels()`](#method-attributeLabels) | Returns the attribute labels. | `DynamicModel` |
| [`attributes()`](#method-attributes) | Returns the list of attribute names. | `DynamicModel` |
| <span id="method-beforeValidate"></span>[`beforeValidate()`](/docs/api/yii/base/model/index.md#method-beforeValidate) | This method is invoked before validation starts. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`canGetProperty()`](#method-canGetProperty) | Returns a value indicating whether a property can be read. | `DynamicModel` |
| [`canSetProperty()`](#method-canSetProperty) | Returns a value indicating whether a property can be set. | `DynamicModel` |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-clearErrors"></span>[`clearErrors()`](/docs/api/yii/base/model/index.md#method-clearErrors) | Removes errors for all attributes or a single attribute. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-createValidators"></span>[`createValidators()`](/docs/api/yii/base/model/index.md#method-createValidators) | Creates validator objects based on the validation rules specified in [`rules()`](/docs/api/yii/base/model/index.md#method-rules). | [`Model`](/docs/api/yii/base/model/index.md) |
| [`defineAttribute()`](#method-defineAttribute) | Defines an attribute. | `DynamicModel` |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-extraFields"></span>[`extraFields()`](/docs/api/yii/base/arrayabletrait/index.md#method-extraFields) | Returns the list of fields that can be expanded further and returned by [`toArray()`](/docs/api/yii/base/arrayabletrait/index.md#method-toArray). | [`ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md) |
| <span id="method-fields"></span>[`fields()`](/docs/api/yii/base/model/index.md#method-fields) | Returns the list of fields that should be returned by default by [`toArray()`](/docs/api/yii/base/arrayabletrait/index.md#method-toArray) when no specific fields are specified. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-formName"></span>[`formName()`](/docs/api/yii/base/model/index.md#method-formName) | Returns the form name that this model class should use. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-generateAttributeLabel"></span>[`generateAttributeLabel()`](/docs/api/yii/base/model/index.md#method-generateAttributeLabel) | Generates a user friendly attribute label based on the give attribute name. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getActiveValidators"></span>[`getActiveValidators()`](/docs/api/yii/base/model/index.md#method-getActiveValidators) | Returns the validators applicable to the current [`$scenario`](/docs/api/yii/base/model/index.md#property-scenario). | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getAttributeHint"></span>[`getAttributeHint()`](/docs/api/yii/base/model/index.md#method-getAttributeHint) | Returns the text hint for the specified attribute. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getAttributeLabel"></span>[`getAttributeLabel()`](/docs/api/yii/base/model/index.md#method-getAttributeLabel) | Returns the text label for the specified attribute. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getAttributes"></span>[`getAttributes()`](/docs/api/yii/base/model/index.md#method-getAttributes) | Returns attribute values. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getErrors"></span>[`getErrors()`](/docs/api/yii/base/model/index.md#method-getErrors) | Returns the errors for all attributes or a single attribute. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getErrorSummary"></span>[`getErrorSummary()`](/docs/api/yii/base/model/index.md#method-getErrorSummary) | Returns the errors for all attributes as a one-dimensional array. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getFirstError"></span>[`getFirstError()`](/docs/api/yii/base/model/index.md#method-getFirstError) | Returns the first error of the specified attribute. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getFirstErrors"></span>[`getFirstErrors()`](/docs/api/yii/base/model/index.md#method-getFirstErrors) | Returns the first error of every attribute in the model. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getIterator"></span>[`getIterator()`](/docs/api/yii/base/model/index.md#method-getIterator) | Returns an iterator for traversing the attributes in the model. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getScenario"></span>[`getScenario()`](/docs/api/yii/base/model/index.md#method-getScenario) | Returns the scenario that this model is used in. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getValidators"></span>[`getValidators()`](/docs/api/yii/base/model/index.md#method-getValidators) | Returns all the validators declared in [`rules()`](/docs/api/yii/base/model/index.md#method-rules). | [`Model`](/docs/api/yii/base/model/index.md) |
| [`hasAttribute()`](#method-hasAttribute) | Returns a value indicating whether the model has an attribute with the specified name. | `DynamicModel` |
| <span id="method-hasErrors"></span>[`hasErrors()`](/docs/api/yii/base/model/index.md#method-hasErrors) | Returns a value indicating whether there is any validation error. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-instance"></span>[`instance()`](/docs/api/yii/base/staticinstancetrait/index.md#method-instance) static | Returns static class instance, which can be used to obtain meta information. | [`StaticInstanceTrait`](/docs/api/yii/base/staticinstancetrait/index.md) |
| <span id="method-isAttributeActive"></span>[`isAttributeActive()`](/docs/api/yii/base/model/index.md#method-isAttributeActive) | Returns a value indicating whether the attribute is active in the current scenario. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-isAttributeRequired"></span>[`isAttributeRequired()`](/docs/api/yii/base/model/index.md#method-isAttributeRequired) | Returns a value indicating whether the attribute is required. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-isAttributeSafe"></span>[`isAttributeSafe()`](/docs/api/yii/base/model/index.md#method-isAttributeSafe) | Returns a value indicating whether the attribute is safe for massive assignments. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-load"></span>[`load()`](/docs/api/yii/base/model/index.md#method-load) | Populates the model with input data. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-loadMultiple"></span>[`loadMultiple()`](/docs/api/yii/base/model/index.md#method-loadMultiple) static | Populates a set of models with the data from end user. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-offsetExists"></span>[`offsetExists()`](/docs/api/yii/base/model/index.md#method-offsetExists) | Returns whether there is an element at the specified offset. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-offsetGet"></span>[`offsetGet()`](/docs/api/yii/base/model/index.md#method-offsetGet) | Returns the element at the specified offset. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-offsetSet"></span>[`offsetSet()`](/docs/api/yii/base/model/index.md#method-offsetSet) | Sets the element at the specified offset. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-offsetUnset"></span>[`offsetUnset()`](/docs/api/yii/base/model/index.md#method-offsetUnset) | Sets the element value at the specified offset to null. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-onUnsafeAttribute"></span>[`onUnsafeAttribute()`](/docs/api/yii/base/model/index.md#method-onUnsafeAttribute) | This method is invoked when an unsafe attribute is being massively assigned. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-rules"></span>[`rules()`](/docs/api/yii/base/model/index.md#method-rules) | Returns the validation rules for attributes. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-safeAttributes"></span>[`safeAttributes()`](/docs/api/yii/base/model/index.md#method-safeAttributes) | Returns the attribute names that are safe to be massively assigned in the current scenario. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-scenarios"></span>[`scenarios()`](/docs/api/yii/base/model/index.md#method-scenarios) | Returns a list of scenarios and the corresponding active attributes. | [`Model`](/docs/api/yii/base/model/index.md) |
| [`setAttributeLabel()`](#method-setAttributeLabel) | Sets a label for a single attribute. | `DynamicModel` |
| [`setAttributeLabels()`](#method-setAttributeLabels) | Sets the labels for all attributes. | `DynamicModel` |
| <span id="method-setAttributes"></span>[`setAttributes()`](/docs/api/yii/base/model/index.md#method-setAttributes) | Sets the attribute values in a massive way. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-setScenario"></span>[`setScenario()`](/docs/api/yii/base/model/index.md#method-setScenario) | Sets the scenario for the model. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-toArray"></span>[`toArray()`](/docs/api/yii/base/arrayabletrait/index.md#method-toArray) | Converts the model into an array. | [`ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`undefineAttribute()`](#method-undefineAttribute) | Undefines an attribute. | `DynamicModel` |
| <span id="method-validate"></span>[`validate()`](/docs/api/yii/base/model/index.md#method-validate) | Performs the data validation. | [`Model`](/docs/api/yii/base/model/index.md) |
| [`validateData()`](#method-validateData) static | Validates the given data with the specified validation rules. | `DynamicModel` |
| <span id="method-validateMultiple"></span>[`validateMultiple()`](/docs/api/yii/base/model/index.md#method-validateMultiple) static | Validates multiple models. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-extractFieldsFor"></span>[`extractFieldsFor()`](/docs/api/yii/base/arrayabletrait/index.md#method-extractFieldsFor) protected | Extract nested fields from a fields collection for a given root field Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "id". | [`ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md) |
| <span id="method-extractRootFields"></span>[`extractRootFields()`](/docs/api/yii/base/arrayabletrait/index.md#method-extractRootFields) protected | Extracts the root field names from nested fields. | [`ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md) |
| <span id="method-resolveFields"></span>[`resolveFields()`](/docs/api/yii/base/arrayabletrait/index.md#method-resolveFields) protected | Determines which fields can be returned by [`toArray()`](/docs/api/yii/base/arrayabletrait/index.md#method-toArray). | [`ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md) |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_BEFORE_VALIDATE"></span>[`EVENT_BEFORE_VALIDATE`](/docs/api/yii/base/model/index.md#event-EVENT_BEFORE_VALIDATE) | [`ModelEvent`](/docs/api/yii/base/modelevent/index.md) | An event raised at the beginning of [`validate()`](/docs/api/yii/base/model/index.md#method-validate). Event name: `'beforeValidate'`. | [`Model`](/docs/api/yii/base/model/index.md) |
| <span id="event-EVENT_AFTER_VALIDATE"></span>[`EVENT_AFTER_VALIDATE`](/docs/api/yii/base/model/index.md#event-EVENT_AFTER_VALIDATE) | [`Event`](/docs/api/yii/base/event/index.md) | An event raised at the end of [`validate()`](/docs/api/yii/base/model/index.md#method-validate) Event name: `'afterValidate'`. | [`Model`](/docs/api/yii/base/model/index.md) |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-SCENARIO_DEFAULT"></span>[`SCENARIO_DEFAULT`](/docs/api/yii/base/model/index.md#const-SCENARIO_DEFAULT) | `'default'` | The name of the default scenario. | [`Model`](/docs/api/yii/base/model/index.md) |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(array<string, mixed>|string[] $attributes = [], array<string, mixed> $config = [])
```

Constructor.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attributes` | `array<string, mixed>\|string[]` | The attributes (name-value pairs, or names) being defined. |
| `$config` | `array<string, mixed>` | The configuration array to be applied to this object. |

### __get() <span id="method-__get"></span>

```php
public function __get(mixed $name): mixed
```

Returns the value of a component property.

This method will check in the following order and act accordingly:

 - a property defined by a getter: return the getter result
 - a property of a behavior: return the behavior property value

Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing `$value = $component->property;`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The property name |

**Returns**

`mixed`: The property value or the value of a behavior's property

**Throws**

- [`UnknownPropertyException`](/docs/api/yii/base/unknownpropertyexception/index.md): if the property is not defined
- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the property is write-only.

### __isset() <span id="method-__isset"></span>

```php
public function __isset(mixed $name): bool
```

Checks if a property is set, i.e. defined and not null.

This method will check in the following order and act accordingly:

 - a property defined by a setter: return whether the property is set
 - a property of a behavior: return whether the property is set
 - return `false` for non existing properties

Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing `isset($component->property)`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The property name or the event name |

**Returns**

`bool`: Whether the named property is set

### __set() <span id="method-__set"></span>

```php
public function __set(mixed $name, mixed $value): mixed
```

Sets the value of a component property.

This method will check in the following order and act accordingly:

 - a property defined by a setter: set the property value
 - an event in the format of "on xyz": attach the handler to the event "xyz"
 - a behavior in the format of "as xyz": attach the behavior named as "xyz"
 - a property of a behavior: set the behavior property value

Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing `$component->property = $value;`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The property name or the event name |
| `$value` | `mixed` | The property value |

**Returns**

`mixed`

**Throws**

- [`UnknownPropertyException`](/docs/api/yii/base/unknownpropertyexception/index.md): if the property is not defined
- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the property is read-only.

### __unset() <span id="method-__unset"></span>

```php
public function __unset(mixed $name): mixed
```

Sets a component property to be null.

This method will check in the following order and act accordingly:

 - a property defined by a setter: set the property value to be null
 - a property of a behavior: set the property value to be null

Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing `unset($component->property)`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The property name |

**Returns**

`mixed`

**Throws**

- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the property is read only.

### addRule() <span id="method-addRule"></span>

```php
public function addRule(string|array $attributes, string|yii\validators\Validator|Closure $validator, array $options = []): $this
```

Adds a validation rule to this model.

You can also directly manipulate [`$validators`](/docs/api/yii/base/model/index.md#property-validators) to add or remove validation rules.
This method provides a shortcut.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attributes` | `string\|array` | The attribute(s) to be validated by the rule. |
| `$validator` | `string\|`[`yii\validators\Validator`](/docs/api/yii/validators/validator/index.md)`\|`[`Closure`](https://www.php.net/manual/en/class.closure.php) | The validator. This can be either: * a built-in validator name listed in `builtInValidators`; * a method name of the model class; * an anonymous function; * a validator class name. * a Validator. |
| `$options` | `array` | The options (name-value pairs) to be applied to the validator. |

**Returns**

`$this`

### attributeLabels() <span id="method-attributeLabels"></span>

```php
public function attributeLabels(): array<string, string>
```

Returns the attribute labels.

Attribute labels are mainly used for display purpose. For example, given an attribute
`firstName`, we can declare a label `First Name` which is more user-friendly and can
be displayed to end users.

By default an attribute label is generated using [`generateAttributeLabel()`](/docs/api/yii/base/model/index.md#method-generateAttributeLabel).
This method allows you to explicitly specify attribute labels.

Note, in order to inherit labels defined in the parent class, a child class needs to
merge the parent labels with child labels using functions such as `array_merge()`.

**Returns**

`array<string, string>`: Attribute labels (name => label)

### attributes() <span id="method-attributes"></span>

```php
public function attributes(): string[]
```

Returns the list of attribute names.

By default, this method returns all public non-static properties of the class.
You may override this method to change the default behavior.

**Returns**

`string[]`: List of attribute names.

### canGetProperty() <span id="method-canGetProperty"></span>

```php
public function canGetProperty(mixed $name, mixed $checkVars = true, mixed $checkBehaviors = true): bool
```

Returns a value indicating whether a property can be read.

A property can be read if:

- the class has a getter method associated with the specified name
  (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when `$checkVars` is true);
- an attached behavior has a readable property of the given name (when `$checkBehaviors` is true).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The property name |
| `$checkVars` | `mixed` | Whether to treat member variables as properties |
| `$checkBehaviors` | `mixed` | Whether to treat behaviors' properties as properties of this component |

**Returns**

`bool`: Whether the property can be read

### canSetProperty() <span id="method-canSetProperty"></span>

```php
public function canSetProperty(mixed $name, mixed $checkVars = true, mixed $checkBehaviors = true): bool
```

Returns a value indicating whether a property can be set.

A property can be written if:

- the class has a setter method associated with the specified name
  (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when `$checkVars` is true);
- an attached behavior has a writable property of the given name (when `$checkBehaviors` is true).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The property name |
| `$checkVars` | `mixed` | Whether to treat member variables as properties |
| `$checkBehaviors` | `mixed` | Whether to treat behaviors' properties as properties of this component |

**Returns**

`bool`: Whether the property can be written

### defineAttribute() <span id="method-defineAttribute"></span>

```php
public function defineAttribute(string $name, mixed $value = null): mixed
```

Defines an attribute.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The attribute name. |
| `$value` | `mixed` | The attribute value. |

**Returns**

`mixed`

### hasAttribute() <span id="method-hasAttribute"></span>

```php
public function hasAttribute(string $name): bool
```

Returns a value indicating whether the model has an attribute with the specified name.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the attribute. |

**Returns**

`bool`: Whether the model has an attribute with the specified name.

**Since**

2.0.16

### setAttributeLabel() <span id="method-setAttributeLabel"></span>

```php
public function setAttributeLabel(string $attribute, string $label): $this
```

Sets a label for a single attribute.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attribute` | `string` | Attribute name. |
| `$label` | `string` | Attribute label value. |

**Returns**

`$this`

**Since**

2.0.35

### setAttributeLabels() <span id="method-setAttributeLabels"></span>

```php
public function setAttributeLabels(string[] $labels = []): $this
```

Sets the labels for all attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$labels` | `string[]` | Attribute labels. |

**Returns**

`$this`

**Since**

2.0.35

### undefineAttribute() <span id="method-undefineAttribute"></span>

```php
public function undefineAttribute(string $name): mixed
```

Undefines an attribute.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The attribute name. |

**Returns**

`mixed`

### validateData() <span id="method-validateData"></span>

```php
public static function validateData(array $data, array $rules = []): static
```

Validates the given data with the specified validation rules.

This method will create a DynamicModel instance, populate it with the data to be validated,
create the specified validation rules, and then validate the data using these rules.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `array` | The data (name-value pairs) to be validated. |
| `$rules` | `array` | The validation rules. Please refer to [`Model::rules()`](/docs/api/yii/base/model/index.md#method-rules) on the format of this parameter. |

**Returns**

`static`: The model instance that contains the data being validated.

**Throws**

- [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a validation rule is not specified correctly.