Magento下面的代码为Customer实体添加了性别属性,有两个可选值 Male和Female 。
$installer->startSetup();
$installer->addAttribute('customer', 'gender', array(
'label' => 'Gender', 'visible' => true, 'required' => false, 'type' => 'int', 'input' => 'select', 'source' => 'eav/entity_attribute_source_table', )); $tableOptions = $installer->getTable('eav_attribute_option'); $tableOptionValues = $installer->getTable('eav_attribute_option_value');
// add options for level of politeness
$attributeId = (int)$installer->getAttribute('customer', 'gender', 'attribute_id'); foreach (array('Male', 'Female') as $sortOrder => $label) {
// add option
$data = array( 'attribute_id' => $attributeId, 'sort_order' => $sortOrder, ); $installer->getConnection()->insert($tableOptions, $data);
// add option label
$optionId = (int)$installer->getConnection()->lastInsertId($tableOptions, 'option_id'); $data = array( 'option_id' => $optionId, 'store_id' => 0, 'value' => $label, ); $installer->getConnection()->insert($tableOptionValues, $data);
}
$installer->endSetup();
(责任编辑:最模板) |