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

前端之家收集整理的这篇文章主要介绍了php – Magento:自定义产品属性,用于选择CMS页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想添加一个新的产品属性,该属性呈现一个包含所有CMS页面的下拉列表作为其选项.
我创建了一个扩展,并希望通过安装脚本添加属性.但是在运行之后,Magento破坏了eav_entity_type表……

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

mysql4安装-0.1.0.PHP

@H_404_6@$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类的源代码

@H_404_6@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表.关于发生的事情,没有错误信息也没有日志条目.

我该如何创建这样的属性

谢谢,
aeno

$installer类本身应该能够添加属性,我不认为你需要加载Entity_Setup类. @H_404_6@$installer = $this; $installer->startSetup(); $installer->addAttribute('catalog_product',)); $installer->endSetup();

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

猜你在找的PHP相关文章