2

The problem: I open a drush php:cli shell and run the following:

>>> \Drupal::entityTypeManager()->getStorage('paragraph')->load(477)->get('field_end_time')->getValue()[0]['value'];
=> "05:01"

I then change the value in the database to 05:02 via the admin panel (content section).

But if I run the same command again in the shell, I still get 05:01.

If I close the shell and open a new one I get 05:02. How do I demand the latest data without terminating the process and starting over? This seems like it must be a basic thing but I've searched far and wide and tried clearing caches and etc. but no dice.

This is affecting me because I have a PHP process that is started by cron that handles a Websocket connections and the same thing is happening there—it's getting stale data from the database.

Thanks for the help.

5
  • Welcome to Drupal Answers! 👋🏿 Probably cache related. I guess when you drupal_flush_all_caches(); in between you will get the right value, but that's probably not what you want? What exactly are you updating that value for? Why a Paragraph when it looks more like config? Can you maybe add some more context? Many thanks 😊
    – leymannx
    Commented Aug 5, 2020 at 20:41
  • 1
    Wow you're right! But that method takes like 5 seconds! This is some legacy code I'm dealing with here. Paragraphs hold some data about a discussion including its closing time.
    – smoyth
    Commented Aug 5, 2020 at 20:43
  • What happens when you use the CLI to also set the value instead of doing that directly in the database?
    – leymannx
    Commented Aug 5, 2020 at 20:43
  • Is there a way to tell which cache is causing this behavior and clear only that one?
    – smoyth
    Commented Aug 5, 2020 at 20:45
  • 1
    I'm not sure how to set the value from the CLI honestly but that is not how it would be happening.
    – smoyth
    Commented Aug 5, 2020 at 20:58

1 Answer 1

4

You can clear the cached entity in memory to get the latest field values:

\Drupal::entityTypeManager()->getStorage('paragraph')->resetCache([477]);

See Referenced entities show old value when changing properties

2
  • Glorious. Thank you!
    – smoyth
    Commented Aug 6, 2020 at 13:44
  • @smoyth – Avoid thank you comments. Accept the answer instead. 🙂
    – leymannx
    Commented Aug 8, 2020 at 21:21

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.