Magento:将静态属性添加到catalog_product,但可以';t确保将值保存到它
很多小时以来,我一直试图找出Magento静态属性的奇怪行为 过了一会儿,我找到了一种在安装脚本中添加静态属性的可靠方法Magento:将静态属性添加到catalog_product,但可以';t确保将值保存到它,magento,magento-1.7,Magento,Magento 1.7,很多小时以来,我一直试图找出Magento静态属性的奇怪行为 过了一会儿,我找到了一种在安装脚本中添加静态属性的可靠方法 $installer = $this; $setup = Mage::getModel( 'eav/entity_setup', 'core_setup' ); $installer->startSetup(); // adding the color code attribute as static attribute $setup->addAttribu
$installer = $this;
$setup = Mage::getModel( 'eav/entity_setup', 'core_setup' );
$installer->startSetup();
// adding the color code attribute as static attribute
$setup->addAttribute( 'catalog_product', 'color_code', array(
'group' => 'General',
'label' => 'Color Code',
'note' => 'the color code',
'type' => 'static',
'input' => 'text',
'backend' => 'eav/entity_attribute_backend_default',
'source' => '',
'frontend' => '',
'required' => false,
'filterable' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
) );
$installer->getConnection()->addColumn(
$installer->getTable( 'catalog/product' ),
'color_code',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'length' => 64,
'comment' => 'Color Code'
)
);
$installer->endSetup();
我想做的第一件事是:有必要自己将列添加到实体表中。。。好的,我做到了。
给定的脚本允许我在管理后端有正确的输入,如果指定列中有值,它将出现在管理后端的相应输入字段中。。。那太好了:-)
现在。。。伟大的奥秘:-(
如果我试图将一个值保存到新属性,它将不起作用。这不是完全正确的。开始时,它不会保留新值。在调试整个Magento之后,将其下放到Varien和Zend类中,以找到它突然起作用的原因,而不更改代码中的任何内容。在调试时,我经常重置模块并且删除并读取了属性,还清除了缓存并重新索引了目录。现在,即使属性被一次又一次地删除和读取,它也可以完美地工作
为了理解发生了什么,我尝试了一个新的属性foo
,同样的奇怪行为发生了…在多次调试、检查、咖啡后…它突然也起作用了
对于具有测试属性bar
,foobar
,argh
,grmph
…的所有下一个测试用例,一次又一次地发生完全相同的情况
好的,让我们将安装脚本推到另一个Magento安装,并且…?!…是的,相同的o.o
这样,我无法确保我的安装脚本执行以下操作:-(
信息:我需要静态属性,以避免在查找具有相同属性(颜色代码)值的其他产品时出现不必要的连接。静态属性基本上可以在每个集合中使用,而无需向集合添加特殊属性。获得了它
只是需要补充一下
$installer->getConnection()->resetDdlCache();
在安装脚本的末尾
$installer = $this;
$setup = Mage::getModel( 'eav/entity_setup', 'core_setup' );
$installer->startSetup();
// adding the color code attribute as static attribute
$setup->addAttribute( 'catalog_product', 'color_code', array(
'group' => 'General',
'label' => 'Color Code',
'note' => 'the color code',
'type' => 'static',
'input' => 'text',
'backend' => 'eav/entity_attribute_backend_default',
'source' => '',
'frontend' => '',
'required' => false,
'filterable' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
) );
$installer->getConnection()->addColumn(
$installer->getTable( 'catalog/product' ),
'color_code',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'length' => 64,
'comment' => 'Color Code'
)
);
$installer->endSetup();