我已创建自定义的我模块的属性类别安装脚本,像这样:Magento的loadByAttribute自定义类别属性
$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条目,而我的自定义字段不是?
如何通过自定义属性加载类别?
我试过了,仍然没有运气。我认为store_id与该属性是否可访问有关。就像我说的,添加store_id = 0的条目允许我按预期加载。 – chris 2010-05-21 23:22:49