【问题标题】:Magento: Custom product attribute to select a CMS pageMagento:自定义产品属性以选择 CMS 页面
【发布时间】:2012-01-18 08:26:18
【问题描述】:

我想添加一个新的产品属性,该属性呈现一个包含所有 CMS 页面作为其选项的下拉列表。 我创建了一个扩展并想通过安装脚本添加这个属性。但是在运行这个之后,Magento 损坏了 eav_entity_type 表......

到目前为止我尝试了什么:

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();

cmspageselect/entity_source 类的来源:

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;
    }
}

正如我所说,Magento 没有创建新属性“test_report”,而是破坏了 eav_entity_type 表。没有关于发生的事情的错误消息或日志条目。

如何创建这样的属性?

谢谢, 艾诺

【问题讨论】:

  • 能否提供您的 cmspageselect/entity_source 类的源代码?
  • 我已编辑问题以包含来源
  • 如何确定表已损坏?

标签: php magento attributes


【解决方案1】:

$installer类本身应该可以添加属性,我不认为你需要加载Entity_Setup类。

$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();

除此之外,我认为您的源值不正确,根据数组中的源元素判断,您应该将模型命名为Mandarin_CMSPageSelect_Model_Entity_Source。我假设您已经在 config.xml 中添加了适当的 <models> 元素,以便告诉 Magento 从哪里加载您的模型。

【讨论】:

  • 感谢您的回答,Cags。但这并不能解决问题。使用您的代码时,我得到Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute()。我什至像你指出的那样重命名了我的源模型。
  • 当使用 $setup->addAttribute() 时,它终于奏效了。我不得不稍微调整一下我的源模型,因为 setPositionOrder() 和 setStoreFilter() 不能在 cms/page_collection 中使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
相关资源
最近更新 更多