如何以编程方式添加属性
我想向产品添加属性,并且我想以编程方式执行此操作。所以我在我的模块中添加了 mysql-install-0.1.0.php ,并添加了这个(受现有示例的启发):
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'collection', array(
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Collection',
'input' => 'text',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => false,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'apply_to' => '',
'is_configurable' => false
));
$installer->endSetup();
语法似乎没问题,但是当执行这部分代码时,问题就来了:
致命错误:调用未定义的方法 Mage_Core_Model_Resource_Setup::addAttribute() 在 /home/frleq/Dev/projets/AVIP/WORKSPACE/avip_magento/app/code/local/Smile/Magentaho/sql/magentaho_setup/mysql4-install-0.1.0.php 第 7 行
你看到哪里出了问题吗?代码并不复杂,它的灵感来自于现有的和常用的......
谢谢
I'd like to add an attribute to products, and I want to do this programmatically. So I added a mysql-install-0.1.0.php to a module of mine, and I added this (inspired from existing examples) :
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'collection', array(
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Collection',
'input' => 'text',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => false,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'apply_to' => '',
'is_configurable' => false
));
$installer->endSetup();
Syntax seems OK, but when it comes to execute this part of the code, here is the problem :
Fatal error: Call to undefined method
Mage_Core_Model_Resource_Setup::addAttribute()
in
/home/frleq/Dev/projets/AVIP/WORKSPACE/avip_magento/app/code/local/Smile/Magentaho/sql/magentaho_setup/mysql4-install-0.1.0.php
on line 7
Do you see what's wrong? Code isn't so complicated, and it is inspired from existing and worling ones...
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用了错误的安装类。检查 config.xml 中的安装程序类声明。您正在使用
Mage_Core_Model_Resource_Setup
。请尝试使用Mage_Eav_Model_Entity_Setup
来代替。You are using the wrong setup class. Check your setup class declaration in config.xml. You are using
Mage_Core_Model_Resource_Setup
. TryMage_Eav_Model_Entity_Setup
instead.如果您想使用与产品相关的选项(可过滤、可搜索等),您应该使用 Mage_Catalog_Model_Resource_Eav_Mysql4_Setup 类。
在最新版本的 Magento 中,您应该使用 Mage_Catalog_Model_Resource_Setup 类。
If you want to use product-related options (filterable, searchable etc.), you should use Mage_Catalog_Model_Resource_Eav_Mysql4_Setup class.
In latest versions of Magento you should use Mage_Catalog_Model_Resource_Setup class.