yiisoft/yii2 · warning · InvalidArgumentException
There is no type mapping for '{attribute}'.
Error message
There is no type mapping for '{attribute}'. What it means
Thrown by yii\behaviors\AttributeTypecastBehavior::typecastAttributes() when it is called with an explicit list of attribute names and one of them has no entry in the behavior's attributeTypes map. The map is either configured by hand or auto-detected from the owner model's validation rules (boolean/number/string validators); names outside the map cannot be cast because their target type is unknown, so the method refuses instead of guessing.
Source
Thrown at framework/behaviors/AttributeTypecastBehavior.php:233
}
}
/**
* Typecast owner attributes according to [[attributeTypes]].
* @param array|null $attributeNames list of attribute names that should be type-casted.
* If this parameter is empty, it means any attribute listed in the [[attributeTypes]]
* should be type-casted.
*/
public function typecastAttributes($attributeNames = null)
{
$attributeTypes = [];
if ($attributeNames === null) {
$attributeTypes = $this->attributeTypes;
} else {
foreach ($attributeNames as $attribute) {
if (!isset($this->attributeTypes[$attribute])) {
throw new InvalidArgumentException("There is no type mapping for '{$attribute}'.");
}
$attributeTypes[$attribute] = $this->attributeTypes[$attribute];
}
}
foreach ($attributeTypes as $attribute => $type) {
$value = $this->owner->{$attribute};
if ($this->skipOnNull && $value === null) {
continue;
}
$this->owner->{$attribute} = $this->typecastValue($value, $type);
}
}
/**
* Casts the given value to the specified type.
* @param mixed $value value to be type-casted.
* @param string|callable $type type name or typecast callable.View on GitHub (pinned to 66f00d18a2)
Solutions
- Add a validation rule for the attribute (safe + a boolean/number/string rule) so auto-detection includes it.
- Or declare the mapping explicitly on the behavior: 'attributeTypes' => ['new_field' => AttributeTypecastBehavior::TYPE_INTEGER].
- Or call typecastAttributes() with no argument (null) to cast only the mapped attributes instead of naming them.
- Fix the name/casing passed to typecastAttributes() to match the rules/map exactly.
Example fix
// before
$model->attachBehavior('typecast', [
'class' => AttributeTypecastBehavior::class,
]);
$model->typecastAttributes(['promo_flags']); // no rule, not in map -> throws
// after
public function rules()
{
return [
['promo_flags', 'integer'], // auto-detection now maps the attribute
];
}
// or explicit: 'attributeTypes' => ['promo_flags' => AttributeTypecastBehavior::TYPE_INTEGER] Defensive patterns
Strategy: validation
Validate before calling
$behavior = $model->getBehavior('typecast');
$names = array_intersect($requestedNames, array_keys($behavior->attributeTypes ?? []));
if (count($names) !== count($requestedNames)) {
throw new \InvalidArgumentException(
'No typecast mapping for: ' . implode(',', array_diff($requestedNames, $names))
);
}
$model->typecastAttributes($names); // safe: only mapped attributes Prevention
- Add a validation rule for every attribute you plan to typecast, or list it explicitly in attributeTypes
- Remember rules with a 'when' condition are excluded from auto-detection — map those attributes by hand
- Call typecastAttributes() with null to cast everything mapped instead of naming attributes
When it happens
Trigger: Calling $model->typecastAttributes(['new_field']) where 'new_field' has no validation rule and no explicit attributeTypes entry; an attribute whose only validator has a 'when' condition (such rules are excluded from auto-detection, so the attribute never lands in the map); passing an attribute name with a typo or different casing than in the rules.
Common situations: Adding a DB column and casting it manually before writing a rule for it; attributes validated only conditionally (when => fn) which detectAttributeTypes() deliberately skips; calling typecast on attributes populated by a relation or magic setter that never participate in rules; typos after renaming an attribute.
Related errors
- Unsupported type '{type}'
- Unknown scenario: $scenario
- Invalid validation rule: a rule must specify both attribute
- The directory does not exist: $path
- Controller class must extend from \yii\base\Controller.
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/9eccde843d494721.
Report an issue: GitHub.