Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I know how I can add a product attribute from an installer script, but how do I tell Magento to use one of the values as default value? (or more values in case it's a multiselect attribute).
I use this code in an installer script to add an attribute with options:

$installer->addAttribute('catalog_product', "some_code_here", array(
    'type'       => 'int', //or varchar if it's multiselect
    'input'      => 'select', //or multiselect
    'label'      => 'Attribute label here',
    'sort_order' => 1000,
    'required'   => false,
    'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'backend'    => 'eav/entity_attribute_backend_array',
    'option'     => array (
        'values' => array(
            0 => 'Value 1',
            1 => 'Value 2',
            2 => 'Value 3',
        )
    ),

)); 

So I need to make the Value 2 be the default value. Or Value 2 and Value 3 in case I create a multiselect attribute

share|improve this question
    
The only way I know is to get the values after creating the attribute, get the id and update the attribute. I think you tried default => 'value 2’? –  Fabian Blechschmidt Jul 22 at 11:30
    
@FabianBlechschmidt. Thanks. I know this approach, but this is what I'm trying to avoid. I want, if possible, to do it from the install script, without any additional selects and updates. I feel dirty doing it otherwise. But if it's not possible I guess I'll have to tackle it like that. –  Marius Jul 22 at 11:32
    
@Marius maybe I am still confused. Is your problem that you have an attribute you add in a set-up script but options are added later via the admin? Or another set-up script? Or something completely different ;) –  David Manners Jul 22 at 11:42
    
@DavidManners. I'm adding an attribute via a setup script and along with the attribute I add some options. And through the same setup script I want to set one of the options as default. This has nothing to do with the admin. Let's say I create the attribute color with options Red, Green, Blue and I want (without admin interaction) the Green option to be the default one. I hope this was a bit clearer. –  Marius Jul 22 at 11:45
1  
@FabianBlechschmidt. Might as well write your comment as an answer. I guess there is no way of doing this from the install script elegantly. –  Marius Jul 22 at 12:03

1 Answer 1

up vote 5 down vote accepted

tl;dr: For select attributes this cannot be declared directly in the initial addAttribute() call; two calls are required.

In order to set the default value, eav_attribute.defaultmust be set with the primary key value from the eav_attribute_option table At a high level the need for two calls is apparent by looking at the flow and function of the addAttribute() and addAttributeOption() methods:

  1. Prepare values
  2. Insert/update into the eav_attribute table
  3. Insert/update set/group
  4. Call addAttributeOption() to insert/update option values

The only hope then would be that addAttributeOption() will be able to update the eav_attribute record. However, there is no code which does this. The only tables accessed by this method are eav_attribute_option and eav_attribute_option_value.

It is therefore necessary to call getAttribute() after the initial addAttribute() call, grab the attribute option ID and then call updateAttribute().

Edit:

After the earlier answer I couldn't help but verify my answer against the existing code; after all, for catalog_product entities we have a nice MVC flow to follow thanks to the attribute CRUD GUI. And there it was, in \Mage_Adminhtml_Catalog_Product_AttributeController::saveAction(), that I saw the attribute model being set with the following data just before being saved:

Option and option configuration data visualized with PhpStorm & Xdebug

Note that the attribute's default value has references to the value keys. Do a little digging and you end up at \Mage_Eav_Model_Resource_Entity_Attribute::_saveOption(), which is called from this resource's _afterSave() method - just as you'd expect given that we need to have an eav_attribute record to update. You can even see how multiselect and select defaults are handled in the same place, just before the final update:

$bind  = array('default_value' => implode(',', $attributeDefaultValue));
$where = array('attribute_id =?' => $object->getId());
$adapter->update($this->getMainTable(), $bind, $where);

So, another alternative is to work directly with the resource model.

share|improve this answer
    
Thanks. I'm very disappointed in this. I took the updateAttribute approach, but there should be a way to do this elegantly. Maybe in Magento 2. oh...and nice explanation. –  Marius Jul 22 at 13:15
    
Thanks, but... too bad I'm wrong! Trying to figure out why though. –  benmarks Jul 22 at 13:29
    
What do you mean you are wrong. To bad you are right :) –  Marius Jul 22 at 13:30
    
Updated - thought I was wrong, but I wasn't. It's possible to work directly with the resource model, and there is a precedent for this in how attributes are saved via the GUI. –  benmarks Jul 22 at 14:27
    
Thanks for the update. Great minds thing alike. I ended up in the same place for inspiration. This is exactly how I handled this in the mean time :) –  Marius Jul 22 at 14:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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