5

I have a client who would like to add the option 'Transgender' in addition to Male and Female in the gender field.

Can I do this directly in the database? Would I need to update anything beyond the following tables:

eav_attribute_option
eav_attribute_option_value

I'm on CE 1.7

Thanks

7

Can you do this in the database? Yes. Should you? No.

Rule #1 about Fight Magento Club - Never touch the database directly.

So create an install script. There are dozens of tutorials out there for that. Once you have that set up, the process isn't dissimilar from adding options to other EAV option types:

<?php


$installer = $this;

/* @var $installer Mage_Customer_Model_Entity_Setup */
$installer->startSetup();

$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');


// add option
$data = array(
    'attribute_id' => 'Non-Binary, Prefer not to say',
    'sort_order'   => 99,
);
$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();
|improve this answer|||||
  • FYI, $label is not defined anywhere. Also, I think the 'attribute_id' value inside of the $data array is wrong, it should be $attributeId. I've posted my working version below. – Louis B. Oct 30 '19 at 14:59
0

I think this is also same as query. Since above code is only about getting those two tables and simply inserting values.THere are no any magento method call in the above script.

If he is a developer with mysql he can do simply through database queries.

|improve this answer|||||
  • When installing Magento, you can set a prefix to use for all magento-managed tables in the database. This allows avoiding conflicts between tables you create yourself. Any Magento-wrapped code will automatically prefix tables when making requests. Besides, you don't want an architecture change to break all previous code. It's easier to wrap a class with an adaptor when the implementation changes than having to rewrite all the SQL. Also, you're supposed to answer the question when you post an answer. This should have been a comment - and when you lack the rep for comments, don't comment. – Domino Mar 28 '17 at 14:25
0

My working version based on @philwinkle's response:

$installer = $this;

$installer->startSetup();

$tableOptions = $installer->getTable('eav_attribute_option');
$tableOptionValues = $installer->getTable('eav_attribute_option_value');

$attributeId = (int) $installer->getAttribute('customer', 'gender', 'attribute_id');
$attributeLabel = 'Diverse';

$data = array(
 'attribute_id' => $attributeId,
 'sort_order' => 99,
);

$installer->getConnection()->insert($tableOptions, $data);

$optionId = (int) $installer->getConnection()->lastInsertId($tableOptions, 'option_id');

$data = array(
    'option_id' => $optionId,
    'store_id' => 0,
    'value' => $attributeLabel,
);

$installer->getConnection()->insert($tableOptionValues, $data);

$installer->endSetup();
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.