0

I'm looking at this code:

$eavSetup->addAttribute(
            Product::ENTITY,
            $code,
            [
                'group'     => 'General',
                'label'     => $code,
                'required'  => false,
                'global'    => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible'   => true,
            ]
        );

At least the first two parameter make sense, but the third one takes in an array of $attr? How do I know what to put into the array? What's the accepted key/value here?

1 Answer 1

0

First, you should examine the column structure of the eav_attribute, eav_attribute_group, and catalog_eav_attribute tables. This array is more or less filling in those columns.

The array keys do not necessarily match the column names in each table, that mapping takes place within Magento. For example, in your array you have the key global which actually maps to the is_global column of the catalog_eav_attribute table. If you can figure out which keys map to which columns, the table structure definition will let you know what values are acceptable.

Here is a more complete array from one of my own scripts to get you on the right track.

[
    'group'                 => 'Some Group',
    'label'                 => 'Uses Domain Registry',
    'type'                  => 'int',
    'input'                 => 'select',
    'source'                => \MageModule\DomainRegistry\Model\Product\Attribute\Source\UsesRegistry::class,
    'backend'               => '',
    'required'              => true,
    'global'                => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
    'visible'               => false,
    'user_defined'          => false,
    'searchable'            => false,
    'filterable'            => false,
    'comparable'            => false,
    'visible_on_front'      => false,
    'unique'                => false,
    'apply_to'              => \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE,
    'is_used_in_grid'       => false,
    'is_visible_in_grid'    => false,
    'is_filterable_in_grid' => false
]

I should also note that the 'source' and 'backend' keys either accept an empty string or the name of a PHP class. If you are creating a text based attribute that is normally typed in, you do not need a source. However, if you have a choice based attribute such as select dropdown menu or multiselect, you will need a source as it will provide the options for your attribute.

The 'backend' key, can accept either an empty string or a PHP class. This is used when you want to perform some additional processing either just before or after saving or just before or after loading the product. Your question doesn't contain enough info for me to determine if you need this additional processing so it should be safe to assume, for now, that you do not need a backend model.

2
  • Hey thanks for helping again! I'm curious what's the thought process on getting the answer. How do you get from trying to find out the parameter required by addAttribute, to looking at a very specific table: eav_attribute
    – farid JAD7
    Commented May 4, 2018 at 2:08
  • Its my pleasure. Part of it is experience. But keep in mind that ALL attributes begin with the eav_attribute table. Some other entities, such as products, have their own table like catalog_eav_attribute which is really nothing more than an extension of the main eav_attribute table. So you could actually do a left join of catalog_eav_attribute to eav_attribute and get the entire thing. The attribute group table deals more with dividing the attributes into tabs or sections in your admin form. Commented May 4, 2018 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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