问题
I have the following code in an installer script & need to now remove the is_school attribute via the installer script, is my code correct?
// 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" => ""
));
My intended approach to remove the attribute - does this look correct?
$installer->startSetup();
$installer->removeAttribute('customer', 'is_school');
$installer->endSetup();
Is there anything else required when removing attributes?
回答1:
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$custAttr = 'is_school';
$setup->removeAttribute('customer', $custAttr);
$setup->endSetup();
Please check class Mage_Eav_Model_Entity_Setup extends Mage_Core_Model_Resource_Setup
In
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;
}
It takes two arguments - the first is the entity code
, and the second is the attribute code
.
回答2:
For that you need to create a different upgrade script. If the install script's version is 0.1.0 then create the file upgrade-0.1.0-0.1.1.php and add the following content in it.
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->removeAttribute('customer', 'is_school');
$installer->endSetup();
Then go to config.xml and change the version tag from 0.1.0 to 0.1.1.
Clear the cache and refresh any page.
来源:https://stackoverflow.com/questions/23775867/removing-a-custom-attribute-in-magento-via-an-installer-script