Whenever you save attribute from the admin panel, Magento will run validation on it and it will change your backend type based on input type. You can see that in getBackendTypeByInput
method inside Mage_Adminhtml_Catalog_Product_AttributeController
class.
/**
* Detect backend storage type using frontend input type
*
* @return string backend_type field value
* @param string $type frontend_input field value
*/
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;
}
You can use this as a guideline when adding new attributes via install script. If you are adding attribute via admin you don't have to worry about that. Of course, if you modify backend or frontend type directly from DB, Magento will validate and 'fix' the attribute on next save.
In other words you should never, ever change attribute properties directly from DB. Especially if the attribute already has some values saved. Doing this will result in attribute values being written in different entity type tables, which is what happened in your case. This will result in various issues, e.g. attribute disappearing from layered navigation, not searchable or filterable, cannot be saved, etc.
Fix for this is simple. First you need to determine what your backend/input types are. Then you need to make sure that the combination is valid using the code above and that it won't be changed in the future by manual editing.
Let's assume you want decimal/price. This means that your values should be saved in catalog_product_entity_decimal
table. You will have to check all the other catalog_product_entity_*
tables and remove all entries associated with your attribute_id
. If you need to preserve the data you can also export rows before you delete them (without value_id
) and import them in catalog_product_entity_decimal
table.
This should fix your attribute. Remember to reindex after this is done.
Best Answer
EAV is a flexible solution to storing data in a database. Relational databases are ideal for situations where there is a fixed number of descriptors (columns) for an entity row. This works well for, say, storing CMS pages or admin user details. But it does not work well for products as they can have a variable (and semi-infinite) number of descriptors (columns). While the data is split up across five or more tables, the end result in the Magento application is that a product is loaded with the set data as if it was one row (Magento handles the saving and loading operation from multiple tables). In other words, EAV stores its relational-columnar information in rows. Instead of data representing the same entity being stored horizontally, it is stored vertically. EAV is made up of three components. The entity, the attribute, and the attribute value.
Entity: EAV stores the primary key in the entity table (for example, catalog_product_entity). Columns in this table are considered static (see the backend_ type column in the eav_attribute table). You can find the entity types in eav_entity_type.
Attribute: Attributes are stored in eav_attribute. Each attribute represents a "column" in the loaded Magento product.
Value: Each attribute has a backend_type. This value determines which table the values for that attribute are stored in. static means those values are stored in the parent _entity table. Otherwise, look for a table with _ entity_ [backend_ type]. The value is the last column. The first columns relate the value to the entity row, store, and the attribute ID.