dongweihuan8832 2012-01-18 08:26
浏览 23
已采纳

Magento:自定义产品属性,用于选择CMS页面

I want to add a new product attribute that renders a drop-down list with all CMS pages as its options. I created an extension and wanted to add this attribute via the install script. but after running this, Magento corrupted the eav_entity_type table...

What I tried so far:

mysql4-install-0.1.0.php:

$installer = $this;

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$installer->startSetup();

$setup->addAttribute('catalog_product', 'test_report', array(
    'label' => 'Test report',
    'required' => false,
    'input' => 'select',
    'source' => 'cmspageselect/entity_source',
    'default' => 'none',
    'position' => 1,
    'sort_order' => 3,
));

$installer->endSetup();

Source for cmspageselect/entity_source class:

class Mandarin_CMSPageSelect_Model_Source extends Mage_Eav_Model_Entity_Attribute_Source_Abstract {
    /**
    * Retrieve Full Option values array
    *
    * @param bool $withEmpty       Add empty option to array
    * @return array
    */
    public function getAllOptions($withEmpty = true)
    {
        $storeId = $this->getAttribute()->getStoreId();
        if (!is_array($this->_options)) {
            $this->_options = array();
        }
        if (!isset($this->_options[$storeId])) {
            $collection = Mage::getResourceModel('cms/page_collection')
                ->setPositionOrder('asc')
                ->setStoreFilter($this->getAttribute()->getStoreId())
                ->load();
            $this->_options[$storeId] = $collection->toOptionIdArray();
        }

        $options = $this->_options[$storeId];
        if ($withEmpty) {
            array_unshift($options, array('label' => '', 'value' => ''));
        }

        return $options;
    }

    /**
    * Get a text for option value
    *
    * @param string|integer $value
    * @return string
    */
    public function getOptionText($value)
    {
        $isMultiple = false;
        if (strpos($value, ',')) {
            $isMultiple = true;
            $value = explode(',', $value);
        }

        $options = $this->getAllOptions(false);

        if ($isMultiple) {
            $values = array();
            foreach ($options as $item) {
                if (in_array($item['value'], $value)) {
                    $values[] = $item['label'];
                }
            }
            return $values;
        }

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

As I said, instead of creating the new attribute "test_report", Magento just corrupted the eav_entity_type table. There was no error message nor log entry about what happened.

How can I create such an attribute?

Thanks, aeno

  • 写回答

1条回答 默认 最新

  • dongxuxian6930 2012-01-26 13:49
    关注

    The $installer class itself should be able to add the attribute, I don't believe you need to load the Entity_Setup class.

    $installer = $this;
    $installer->startSetup();
    
    $installer->addAttribute('catalog_product', 'test_report', array(
        'label' => 'Test report',
        'required' => false,
        'input' => 'select',
        'source' => 'cmspageselect/entity_source',
        'default' => 'none',
        'position' => 1,
        'sort_order' => 3,
    ));
    
    $installer->endSetup();
    

    Other than that I think your source value is incorrect, judging by the source element in the array, you should be naming your model Mandarin_CMSPageSelect_Model_Entity_Source. I am assuming that you have added in the appropriate <models> element into config.xml, in order to tell Magento where to load your model from.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?