我在模块的安装脚本中为一个类别创建了自定义属性,如下所示:
$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);
?>
我没有结果。但是,如果我使用设置为“测试类别”的“名称”字段加载ByAttribute,我会得到一个结果。
因此,在数据库中,我查看了catalog_category_entity_varchar
表并注意到“name”属性有一个 store_id = 0 和 store_id = 1 的条目,而“custom_field”属性只有一个 store_id = 1 的条目。
当我在表中为“custom_field”添加了一个 store_id = 0 条目并将值设置为“abcd”时catalog_category_entity_varchar
,loadByAttribute 得到了预期的结果。
我的问题是,为什么“名称”字段中有一个 store_id = 0 条目,catalog_category_entity_varchar
而我的自定义字段却没有?
如何按自定义属性加载类别?