我在模块的安装脚本中为类别创建了自定义属性,如下所示:
$attrib = array(
'type' => 'varchar',
'group' => 'My Data',
'backend' => '',
'frontend' => '',
'label' => 'My Custom Field',
'input' => 'text',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => true,
);
$installer->addAttribute(3, 'custom_field', $attrib);
该字段在管理中显示正常,当我在脚本中创建类别时,如下所示:
$p_category = Mage::getModel('catalog/category')
->setStoreId(0)
->load(2);
$category = Mage::getModel('catalog/category');
$category->setStoreId(0)
->setName('Test Category')
->setCustomField('abcd')
->setDisplayMode('PRODUCTS')
->setAttributeSetId($category->getDefaultAttributeSetId())
->setIsActive(1)
->setIsAnchor(1)
->setPath(implode('/',$p_category->getPathIds()))
->setInitialSetupFlag(true)
->save();
我可以在 Magneto 管理界面中看到值“abcd”。但是当我调用下面的代码时:
<?php
$category = Mage::getModel('catalog/category')->loadByAttribute('custom_field', 'abcd');
print_r($category);
?>
我没有得到任何结果。但是,如果我使用 loadByAttribute 将“名称”字段设置为“测试类别”,我确实会得到结果。
因此,在数据库中,我查看了 catalog_category_entity_varchar
表,注意到“name”属性有一个 store_id = 0 和 store_id = 1 的条目,而“custom_field”属性只有一个条目store_id = 1 的条目。
当我在 catalog_category_entity_varchar
表中为“custom_field”添加 store_id = 0 条目并将值设置为“abcd”时,loadByAttribute 获得了预期结果。
我的问题是,为什么“name”字段在 catalog_category_entity_varchar
中获得 store_id = 0 条目,而我的自定义字段却没有?
如何通过自定义属性加载类别?
最佳答案
如果您将以下键更改为全局,那么它应该为两个商店添加它
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
关于Magento loadByAttribute 用于自定义类别属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2886123/