2

I am trying to create a custom Eav entity in Magento 2.

I have gone through two core eav entity modules in Magento which are Catalog and Customer.

There are a couple of things which I found strange in these two modules.

Setup Files

'entity_type_id' => self::CATEGORY_ENTITY_TYPE_ID

'entity_type_id' => \Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER

It seems like Magento sets entity_type_id statically here.

Q1: Why Magento sets entity_type_id statically here though it manages dynamically within eav_entity_type table?

Please see reference image

Q2: When are the records in eav_entity_type table created?

Q3: If I need to create a custom EAV model, the how should I define entity_type_id for that model

3
  • you should define your own constant same as above 2 class. entity_type_id is AUTO_INCREMENT but the purpose to define as constant is to retrieve it easily. i know you are confuse if you have another eav module in that case your id may conflict with it but that is the only way. go with id 9 and in case of conflict you need to just change your constant value. Commented Jun 23, 2017 at 5:03
  • Why go with 9.'entity_type_id' Should be dynamic
    – Amit Bera
    Commented Jun 23, 2017 at 5:15
  • Hello. I got It returns error like : Unknown entity type: VendorName\ModuleName\Model\Mainpage requested error. Do you know it how to solve? magento.stackexchange.com/questions/263579/… Commented Feb 27, 2019 at 12:02

1 Answer 1

1

This is a partial answer. And logically deduced therefor prone to error.

The eav_entity_table is populated at Magento installation. The only way to change existing values in the table si by custom added logic.

They are making the assumption that no one(in their right mind) changes those values. Ergo they extracted the values into constants making the following assumption

No Magento installation will mess with native entity_type_ids of core Magento entities.

The reason is simple to deduce: performance;

You are right in saying that you should treat your custom entity_type_id as a dynamic value because there are other extensions that might have added their own custom-entities so there is no guarantee that 9 will always represent your custom entity for example.

0

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.