Magento 无法在后端保存属性产品值?

Magento 无法在后端保存属性产品值?,magento,attributes,Magento,Attributes,我有一个函数,可以通过编程为我创建如下属性: function createAttribute($code, $label, $attribute_type, $product_type,$attributeSetId) { $_attribute_data = array( 'attribute_code' => $code, 'is_global' => '1', 'frontend_input' => $attribute

我有一个函数,可以通过编程为我创建如下属性:

     function createAttribute($code, $label, $attribute_type, $product_type,$attributeSetId)
    {
    $_attribute_data = array(
    'attribute_code' => $code,
    'is_global' => '1',
    'frontend_input' => $attribute_type, 
    'default_value_text' => '',
    'default_value_yesno' => '0',
    'default_value_date' => '',
    'default_value_textarea' => '',
    'is_unique' => '0',
    'is_required' => '0',
    'apply_to' => array($product_type), 
    'is_configurable' => '0',
    'is_searchable' => '0',
    'is_visible_in_advanced_search' => '0',
    'is_comparable' => '0',
    'is_used_for_price_rules' => '0',
    'is_wysiwyg_enabled' => '0',
    'is_html_allowed_on_front' => '1',
    'is_visible_on_front' => '1',
    'used_in_product_listing' => '0',
    'used_for_sort_by' => '0',
    'is_filterable' => '1',
    'frontend_label' => $label,

);
$model = Mage::getModel('catalog/resource_eav_attribute');
if (!isset($_attribute_data['is_configurable'])) {
    $_attribute_data['is_configurable'] = 0;
}
if (!isset($_attribute_data['is_filterable'])) {
    $_attribute_data['is_filterable'] = 0;
}
if (!isset($_attribute_data['is_filterable_in_search'])) {
    $_attribute_data['is_filterable_in_search'] = 0;
}
if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
    $_attribute_data['backend_type'] = $model->getBackendTypeByInput($_attribute_data['frontend_input']);
}


$model2=Mage::getModel('eav/entity_setup','core_setup');
$attributeGroupId = '';
if($attributeSetId)
{
     $attributeGroup=$model2->getAttributeGroup('catalog_product',$attributeSetId,'Noutati');

if(array_key_exists('attribute_group_id',$attributeGroup))
{
    $attributeGroupId = $attributeGroup['attribute_group_id'];
    $model->setAttributeGroupId($attributeGroupId);
}   
    $model->setAttributeSetId($attributeSetId);
}   





$model->addData($_attribute_data);
$model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
$model->setIsUserDefined(1);



try {
    $model->save();

    $attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$code);
    $attributeId = $attribute->getId();
    assignAttributeToGroup($attributeId,$attributeGroupId,$attributeSetId);

} catch (Exception $e) { echo '<p>Sorry, error occured while trying to save the attribute. Error: '.$e->getMessage().'</p>'; }

 }
  createAttribute('test', 'Label Test', "multiselect", "simple",'77');
如您所见,我要为我的magento存储创建的所有属性都是多选下拉列表,因此我使用以下函数为新创建的属性添加属性值:

 function addAttributeValue($arg_attribute, $arg_value)
{
    $attribute_model        = Mage::getModel('eav/entity_attribute');

    $attribute_code         = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
    $attribute              = $attribute_model->load($attribute_code);

    if(!attributeValueExists($arg_attribute, $arg_value))
    {
        $value['option'] = array($arg_value,$arg_value);
        $result = array('value' => $value);
        $attribute->setData('option',$result);
        $attribute->save();
    }

    $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
    $attribute_table        = $attribute_options_model->setAttribute($attribute);
    $options                = $attribute_options_model->getAllOptions(false);

    foreach($options as $option)
    {
        if ($option['label'] == $arg_value)
        {
            return $option['value'];
        }
    }


return false;
}
我必须这样做,因为当我创建属性时,我不知道确切的值是什么

这两个函数都可以完美地工作,创建属性,将其添加到正确的属性集中,还可以正确添加我想要的属性值,但我面临以下情况:

我创建一个产品,将其添加到我使用上述函数添加新创建属性的集合中,然后该属性会出现在产品编辑页面的后端,但每当我尝试为其保存某些内容时,它都不会保存。页面刷新,来自会话的消息保存为产品,但该产品不存在所选的属性值。实际上,我无法从产品后端编辑页面保存此属性的值

脚本运行后,我刷新了所有索引管理,但问题仍然存在。我还需要提到的是,我以这种方式创建了1000多个属性,每个属性至少有4-5个值,因此数据量非常大……因此我需要以编程方式实现这一点。 如果有人能给我一个提示,说明为什么上面的功能不正常,我将非常感激


非常感谢advace

在不知道具体出了什么问题的情况下,我相信您的问题将通过使用负责处理目录EAV的类来解决,即,-以前称为Mage_catalog_Model_Resource_EAV_Mysql4_Setup。该类及其父类具有CRUD属性和任何枚举值所必需的一切

$setup = Mage::getResourceModel('catalog/setup','catalog_setup');
$setup->addAttribute(
    'catalog_product',
    'your_attr_code',
    array(
        'type'  => 'text',
        'label' => 'Test'
    )
);
使用该类的好处在于它有一个_prepareValues方法,该方法设置目录实体属性ref catalog\u eav\u属性表的特定默认参数以及所有eav属性ref eav\u属性表和父_prepareValues方法的公共参数

理解和示例的最佳来源是查看Mage/Catalog/sql/和Mage/Catalog/data/中的设置脚本