0

I have two custom entities (Car and Car Type) and I am using one as a reference for the other.

When I load the "Car" entity I can only get the ID from "Car Type". I know I can then run entity_load to get the fields on "Car Type", but when I have multiple results of "Car" querying every record become inefficient.

Is there any way to load the "Car Type" entity fields when loading "Car" entity?

1

You might want to take a look at Entity metadata wrappers. They are not really efficient as they load even more data, but they simplify the working with entity properties and fields and also resolve nested entities (so you would be able to do something like $car->field_car_type->field_length->value()).

But under the hood (a car joke :D), it still performs an entity_load to get the values. It is less code for you though.

To answer your question:

Drupal makes the assumption a field is always attached to an entity, it would be very difficult to properly load some fields without actually loading the entity (I suppose that is what you are after). The function which loads field values and attaches those fields to entities (field_attach_load) already required loaded entities as input. (This function is often called during entity_load). You could attempt to replicate its behaviour without loading the entities, but you will probably run into a lot of issues. The same applies for fetching a single field's value; field_get_items() and field_view_value() also expect a loaded entity. So my advise would be to just load the entity, possibly using entity metadata wrappers as they make your life easier.

| 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.