Score:0

How to get fields of a product type using a database dynamic query

kw flag

So I currently have multiple different product types that have varying fields. I was able to fetch all products using EntityTypeManager the following way:

$productStorage = \Drupal::entityTypeManager()->getStorage('commerce_product');
$ids = $productStorage->getQuery()->execute();
$allProducts = $productStorage->loadMultiple($ids);

This works quite well, and I see how this can be used to execute more complicated queries. However, I'd like to now get a certain product (perhaps based off the product_id). I saw a previous post showing that a dynamic query is faster than an EntityFieldQuery. So if for example my custom api call (my drupal is communicating with a frontend server) gets a request for a certain product by product_id and product type, how can I determine the fields of the product type with which I can then create a dynamic database query to get the product and the field values that go along with it? I read somewhere that this information is stored in the config table in the database and shouldn't really be queried, which is why I'm asking this question.

Thanks for the help in advance, and let me know if there's anything unclear about my post!

leymannx avatar
ne flag
`\Drupal::entityTypeManager()->getStorage('commerce_product')->load($id)` I guess I don't understand the question.
Jaypan avatar
de flag
I'm not clear on what you are asking either.
Score:1
cn flag

how can I determine the fields of the product type with which I can then create a dynamic database query to get the product and the field values that go along with it?

You shouldn't do that. That's what the entity API already does. You would just be rebuilding the existing functionality of core Drupal for no benefit. Then there's contrib modules, which extend and augment that functionality. You would need to reproduce everything relevant that they do as well.

This sounds like a classic case of premature optimisation, which rarely has the intended benefits in my experience. Drupal has a great caching system, it would be advisable to look into that as a way to mitigate issues, if and when they actually present as real world problems.

As an aside, as a literal answer to the original question, this is how you get the fields of an entity bundle:

$field_defs = \Drupal::service('entity_field.manager')
  ->getFieldDefinitions('commerce_product', 'bundle');

$fields_names = array_keys($field_defs);
levente.nas avatar
kw flag
Ok I see. I thought there's a significant difference in execution time between the entity API and database query, and it's more worth it to use database queries directly. Thanks for clearing it up... I'll stick to the entity API then.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.