{"record":{"id":"9eccde843d494721","repo":"yiisoft/yii2","slug":"there-is-no-type-mapping-for-attribute","errorCode":null,"errorMessage":"There is no type mapping for '{attribute}'.","messagePattern":"There is no type mapping for '(.+?)'\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"warning","filePath":"framework/behaviors/AttributeTypecastBehavior.php","lineNumber":233,"sourceCode":"        }\n    }\n\n    /**\n     * Typecast owner attributes according to [[attributeTypes]].\n     * @param array|null $attributeNames list of attribute names that should be type-casted.\n     * If this parameter is empty, it means any attribute listed in the [[attributeTypes]]\n     * should be type-casted.\n     */\n    public function typecastAttributes($attributeNames = null)\n    {\n        $attributeTypes = [];\n\n        if ($attributeNames === null) {\n            $attributeTypes = $this->attributeTypes;\n        } else {\n            foreach ($attributeNames as $attribute) {\n                if (!isset($this->attributeTypes[$attribute])) {\n                    throw new InvalidArgumentException(\"There is no type mapping for '{$attribute}'.\");\n                }\n                $attributeTypes[$attribute] = $this->attributeTypes[$attribute];\n            }\n        }\n\n        foreach ($attributeTypes as $attribute => $type) {\n            $value = $this->owner->{$attribute};\n            if ($this->skipOnNull && $value === null) {\n                continue;\n            }\n            $this->owner->{$attribute} = $this->typecastValue($value, $type);\n        }\n    }\n\n    /**\n     * Casts the given value to the specified type.\n     * @param mixed $value value to be type-casted.\n     * @param string|callable $type type name or typecast callable.","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/behaviors/AttributeTypecastBehavior.php#L215-L251","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n$model->attachBehavior('typecast', [\n    'class' => AttributeTypecastBehavior::class,\n]);\n$model->typecastAttributes(['promo_flags']); // no rule, not in map -> throws\n\n// after\npublic function rules()\n{\n    return [\n        ['promo_flags', 'integer'], // auto-detection now maps the attribute\n    ];\n}\n// or explicit: 'attributeTypes' => ['promo_flags' => AttributeTypecastBehavior::TYPE_INTEGER]","handlingStrategy":"validation","validationCode":"$behavior = $model->getBehavior('typecast');\n$names = array_intersect($requestedNames, array_keys($behavior->attributeTypes ?? []));\nif (count($names) !== count($requestedNames)) {\n    throw new \\InvalidArgumentException(\n        'No typecast mapping for: ' . implode(',', array_diff($requestedNames, $names))\n    );\n}\n$model->typecastAttributes($names); // safe: only mapped attributes","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["php","yii2","behavior","model","typecast","validation-rules"],"backgroundTag":"missing-type-mapping","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}