2

I am working on one Drupal 8.3 site. I created my own custom Authentication provider for REST API authorisation.

Following are the "authenticate" method sample code.

/**
   * {@inheritdoc}
   */
  public function authenticate(Request $request) {
      try {

          $uid = 1;// passing 1 for testing.

          $accounts = $this->entityTypeManager->getStorage('user')->loadByProperties(array('uid' => $uid, 'status' => 1));
          $account = reset($accounts);
          if ($account) {
              return $this->entityTypeManager->getStorage('user')->load($uid);
          }
      } 
      return [];
  }

My doubt here is "$this->entityTypeManager->getStorage" is getting data from cache table. Or everytime it connect to db and generate response?

0

2 Answers 2

6

load() and loadMultiple() are cached, loadByProperties() is a shortcut for an entity query + load, the entity query is not cached.

Do not use loadByProperties() if you know the entity ID.

2
  • Really thanks Berdir for explanation. But may you please explain why not to use loadByProperties if knowing entity id. Its not clear this to me. Would you give any reference or more details.
    – vijay
    Commented Jul 1, 2017 at 17:49
  • because you do a query to get the ids. If you already have the id, you can just use load() or loadMultiple().
    – Berdir
    Commented Jul 3, 2017 at 20:30
3

For anyone coming across this issue;

You can also use the entity resetCache without passing an entity ID parameter since the resetCache method can accept empty parameters. See documentation on: EntityStorageBase::resetCache

$entity_storage = \Drupal::entityTypeManager()->getStorage('YOUR_ENTITY_NAME');
$entity_storage->resetCache(); // reset entity to clear cache
$results = $entity_storage->loadByProperties(['uid' => $uid, 'status' => 1]); // load entity data
return $results;

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.