【问题标题】:Get Attribute model by attribute_code in Magento通过 Magento 中的 attribute_code 获取属性模型
【发布时间】:2011-09-26 07:20:02
【问题描述】:

我如何在 Magento 中通过 attribute_code 获得 Attribute model(来自:eav_attribute 表)?
通知:
- 我不在乎 entity_type 是什么。
非常感谢。

【问题讨论】:

    标签: magento


    【解决方案1】:

    您必须知道entity_type,因为您可以为不同的实体拥有相同的attribute_code。所以要获取属性模型:

    $attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type, $attributeCode);
    

    $entity_type参数可以是numeric(id直接),string(例如'catalog_product'Mage_Catalog_Model_Product::ENTITY)或者它可以是模型的实例Mage_Eav_Model_Entity_Type

    【讨论】:

    • 谢谢。我会尝试向您确认结果。
    • 我使用上面的代码来确定 short_description 是否“在前端的产品视图页面上可见”:$attributeShortDescription = Mage::getModel('eav/entity_attribute')->loadByCode('目录产品','short_description'); $attributeShortDescription->getIsVisibleOnFront();
    • 使用常量的版本:$_attr = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
    【解决方案2】:

    也许你可以通过过滤集合来读取属性

       Mage::getModel('eav/entity_attribute')->getCollection()->addFieldToFilter('attribute_code', array('in' => $codes) )
    

    由于我需要通过代码从产品中获取属性,所以我这样做:

        $codes = (array) $codes; 
        $res = array_intersect_key($this->getAttributes(), array_flip($codes));
    

    $codes 是一个属性代码数组 范围:扩展Mage_Catalog_Model_Product

    【讨论】:

      【解决方案3】:
      $attribute_code = "flat_printing_quantity"; 
      $attribute_details =
      Mage::getSingleton("eav/config")->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attribute_code);
      $attribute = $attribute_details->getData();
      echo $attribute['attribute_id'];
      

      【讨论】:

        【解决方案4】:

        $attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode(1, 'is_approved'); echo $attributeModel->getAttributeId();

        【讨论】:

        • 这不仅有特定于许多场景之一的数据,而且还呼应了接受的答案,代码没有格式化,也没有解释。
        猜你喜欢
        • 2020-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-29
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        相关资源
        最近更新 更多