我已創建自定義的我模塊的屬性類別安裝腳本,像這樣: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