Magento – Magento keeps changing an attribute’s backend type from “int” to “varchar”

attributesproduct

In Magento, you can't create a custom attribute of type "decimal" or "price".

But it's possible to create the attribute as text, and change the backend_type in it's database record from "varchar" to "decimal" or "int". (table: eav_attribute)

But, Magento keeps changing it back to "varchar!!" Why?

Best Answer

Magento will validate and change attribute backend_type when you save the attribute from admin panel (Catalog > Attributes > Manage Attributes).

I believe that the install script does not validate this so it's possible, for example, to create select attribute with varchar type. This does not mean it's OK to do it, in fact you should avoid it because you'll end up with a 'broken' attribute.

public function getBackendTypeByInput($type)
{
    $field = null;
    switch ($type) {
        case 'text':
        case 'gallery':
        case 'media_image':
        case 'multiselect':
            $field = 'varchar';
            break;

        case 'image':
        case 'textarea':
            $field = 'text';
            break;

        case 'date':
            $field = 'datetime';
            break;

        case 'select':
        case 'boolean':
            $field = 'int';
            break;

        case 'price':
            $field = 'decimal';
            break;
    }

    return $field;
}