2012-01-18 113 views
7

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

我试了一下,到目前为止:

mysql4安装-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; 
    } 
} 

正如我所说的,而不是创建新属性 “test_report”,Magento的只是败坏了eav_entity_type TA BLE。没有错误信息,也没有记录发生了什么事情。

如何创建这样的属性?

感谢, aeno

+0

你可以提供你的cmspageselect/entity_source类的源代码吗? – 2012-01-18 08:55:12

+0

我已编辑的问题,包括来源 – aeno 2012-01-18 09:11:51

+0

你如何确定表是腐败? – 2012-01-26 13:39:58

回答

4

$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。我假设您已将相应的<models>元素添加到config.xml中,以告诉Magento从哪里加载模型。

+0

感谢您的回答,Cags。但那并不能解决问题。 当使用你的代码时,我得到'调用未定义的方法Mage_Core_Model_Resource_Setup :: addAttribute()'。 我甚至像你指出的那样重新命名我的源模型。 – aeno 2012-01-27 13:39:26

+0

当使用$ setup-> addAttribute()时,它终于奏效了。我不得不调整我的源模型,因为setPositionOrder()和setStoreFilter()不能用在cms/page_collection中。 – aeno 2012-01-27 13:50:26

相关问题