2014-05-21 27 views
2

我在安装程序脚本中有以下代码&现在需要通过安装程序脚本删除is_school属性,我的代码是否正确?通过安装程序脚本删除Magento中的自定义属性

// code within existing installer script (this works fine) 
$installer->addAttribute("customer", "is_school", array(
"type"  => "int", 
"backend" => "", 
"label" => "Is School?", 
"input" => "int", 
"source" => "", 
"visible" => false, 
"required" => false, 
"default" => "", 
"frontend" => "", 
"unique"  => false, 
"note"  => "" 
)); 

我打算删除属性 - 这看起来是否正确?

$installer->startSetup(); 
$installer->removeAttribute('customer', 'is_school'); 
$installer->endSetup(); 

删除属性时是否还有其他要求?

+1

如果'$ installer'是'Mage_Eav_Model_Entity_Setup'的一个实例,'customer'是添加了'is_school'的唯一实体,那么您的删除脚本应该可以正常工作。 –

回答

6
$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 

$custAttr = 'is_school'; 

$setup->removeAttribute('customer', $custAttr); 
$setup->endSetup(); 

请检查class Mage_Eav_Model_Entity_Setup extends Mage_Core_Model_Resource_Setup

public function removeAttribute($entityTypeId, $code) 
{ 
    $mainTable = $this->getTable('eav/attribute'); 
    $attribute = $this->getAttribute($entityTypeId, $code); 
    if ($attribute) { 
     $this->deleteTableRow('eav/attribute', 'attribute_id', $attribute['attribute_id']); 
     if (isset($this->_setupCache[$mainTable][$attribute['entity_type_id']][$attribute['attribute_code']])) { 
      unset($this->_setupCache[$mainTable][$attribute['entity_type_id']][$attribute['attribute_code']]); 
     } 
    } 
    return $this; 
} 

它有两个参数 - 第一个是entity code,第二个是attribute code

0

为此,您需要创建不同的升级脚本。如果安装脚本的版本为0.1.0,则创建文件upgrade-0.1.0-0.1.1.php并在其中添加以下内容。

$installer = $this; 
$installer->startSetup(); 
$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 
$setup->removeAttribute('customer', 'is_school'); 
$installer->endSetup(); 

然后转到config.xml并将版本标记从0.1.0更改为0.1.1。

清除缓存并刷新任何页面。

相关问题