Add new column to an EAV table `catalog_product_entity`
-
16-10-2019 - |
Question
I tried to add new column to the table catalog_product_entity
via a setup resource. But when i try to save a value to table (using setMyColumnName()
), the value is not reflected in the underlying table.
I understand that this is the wrong way to add a column to an eav table in magento. But i couldn't find any article on howto do this.
NOTE: i don't need to add an attribute to catalog/product
. I want to add a field as a column.
Solution
i don't need to add an attribute to catalog/product. I want to add a field as a column
You want to store entity-related data against product records? Then you do want to add an attribute to the product entity. That's what a column on the catalog_product_entity
table is.
Your approach is similar to mine the first time I tried to store a new property against the catalog_product
entity, and with the same results. The reason that your write is failing is that the property stored against your product instance (e.g. my_column_name
via setMyColumnName()
) is not a registered attribute for that entity. When you call $product->save()
, the EAV resource save()
will load each attribute configuration in order to perform the save operation - even for attributes with values stored on the entity table.
The following script will create the column (VARCHAR 255 in this case) as well as register the attribute. You will want to test that the column value is indexed with flat product enabled.
/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
$installer->startSetup();
$installer->getConnection()->addColumn(
$installer->getTable('catalog/product'),
'my_column_name',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'length' => 255,
'comment' => 'My Column Name'
)
);
$installer->addAttribute(
'catalog_product',
'my_column_name',
array(
'label' => 'Initial Label',
'type' => 'static'
)
);
$installer->endSetup();
OTHER TIPS
add attribute to main table
You want to add an attribute backend_type => 'static'
with this, the attribute is added as column to the main table
add value in database
To add the value into the database, magento needs to know, that this column exists. Zend_Db
caches all schemas of the tables and you need to clean the cache, to refresh this schema. So deleting everyhing in var/cache/
should solve your problem