1

I have three custom entities, all generated with Drupal Console. I only added some basicFieldDefinitions to them before testing some CRUD operations on them.

I have these entities:

  • ConfigEntity (config entity - lowest level)
  • ChildEntity (content entity - has an entity_reference field to ConfigEntity)
  • ParentEntity (content entity - has an entity_reference field to ChildEntity)

When I display the name of the ConfigEntity through the entire chain, it displays properly

// "Original Value"
$parentEntity->get('child_entity')->entity->get('config_entity')->entity->label()

When I update the name, it still displays the original value through the chain, although printing it from the entity itself, it is the new value.

// "Original Value"
$parentEntity->get('child_entity')->entity->get('config_entity')->entity->label();

$configEntity = ConfigEntity::load(1);
$configEntity->set('label', "New Value")->save();

$configEntity = ConfigEntity::load(1);

// "New Value"
$configEntity->label();

$parentEntity = ParentEntity::load(1);

// "Original Value" instead of "New Value"
$parentEntity->get('child_entity')->entity->get('config_entity')->entity->label();

Any idea if there's a cache tag mechanism I have to implement manually, or something I have to invalidate myself in code?

1 Answer 1

1

You can clear the static entity cache with EntityStorageBase::resetCache:

\Drupal::entityTypeManager()->getStorage('parent_entity')->resetCache([$parentEntity->id()]);
2
  • Thanks! Is it best to add this to the save() method of the child? Commented Apr 11, 2019 at 6:30
  • Most times this is not necessary, because what you are doing is testing and rarely happens in production where you usually save entities in post requests.
    – 4uk4
    Commented Apr 11, 2019 at 6:46

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.