[英]Magento: Custom product attribute to select a CMS page
I want to add a new product attribute that renders a drop-down list with all CMS pages as its options. 我想添加一个新的产品属性,该属性呈现一个包含所有CMS页面的下拉列表作为其选项。 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...
但是在运行之后,Magento破坏了eav_entity_type表......
What I tried so far: 到目前为止我尝试了什么:
mysql4-install-0.1.0.php: 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();
Source for cmspageselect/entity_source class: 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;
}
}
As I said, instead of creating the new attribute "test_report", Magento just corrupted the eav_entity_type table. 正如我所说,Magento没有创建新属性“test_report”,而是破坏了eav_entity_type表。 There was no error message nor log entry about what happened.
关于发生的事情没有错误信息也没有记录条目。
How can I create such an attribute? 我该如何创建这样的属性?
Thanks, aeno 谢谢,aeno
The $installer
class itself should be able to add the attribute, I don't believe you need to load the Entity_Setup class. $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();
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
. 除此之外我认为您的源值不正确,从数组中的源元素判断,您应该命名您的模型
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. 我假设您已将相应的
<models>
元素添加到config.xml中,以便告诉Magento从哪里加载模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.