【问题标题】:Delete entity under some conditions Drupal 8在某些情况下删除实体 Drupal 8
【发布时间】:2016-04-26 09:14:38
【问题描述】:

任务是删除满足某些指定条件的实体。我该怎么做?

$current_user = \Drupal::currentUser()->id();
$storage = \Drupal::entityManager()->getStorage('context');

$query = $storage->getQuery()->condition('user_id', $current_user);

$query = $storage->getQuery()->delete();
$query->condition('user_id', $current_user);
$query->condition('key_text', $key);

$query->execute();

但代码返回:致命错误:调用未定义的方法 Drupal\Core\Config\Entity\Query\Query::delete()

【问题讨论】:

    标签: entity drupal-8


    【解决方案1】:

    要查询实体,您可以使用 entityQuery,下面的示例使用它。

    // Get all users with email containing "xyz"
    $query = \Drupal::entityQuery('user')
      ->condition('mail', "XYZ", 'CONTAINS');
    $uids = $query->execute();
    
    // Load these entities ($uids) in our case using storage controller.
    // We call loadMultiple method and give $uids array as argument.     
    $itemsToDelete = \Drupal::entityTypeManager()->getStorage('user')
      ->loadMultiple($uids);
    
    // Loop through our entities and deleting them by calling by delete method.
    foreach ($itemsToDelete as $item) {
      $item->delete();
    }
    

    【讨论】:

    • 虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它会是 greatly improved if it included an explanation of how 和 why 这解决了问题。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
    【解决方案2】:

    使用实体存储的delete() 方法删除多个实体。无需遍历所有这些。

    $query = \Drupal::entityQuery('taxonomy_term');
    $query->condition('vid', 'tags');
    $tids = $query->execute();
    
    $storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
    $entities = $storage_handler->loadMultiple($tids);
    $storage_handler->delete($entities);
    

    取自https://stackoverflow.com/a/43786945/442022。

    【讨论】:

    • 如果有很多实体,您将耗尽内存(与接受的答案相同)。有时你只需要一个一个加载和删除。
    【解决方案3】:

    您需要先获取实体,然后才能实际删除它们。更新它们或从它们获取信息也需要此方法。希望下面的代码有所帮助:)

    $current_user = \Drupal::currentUser()->id();    
    
    $ids = \Drupal::entityQuery('context')
              ->condition('user_id', $current_user)
              ->execute();
    
    $itemsToDelete = $this->entityTypeManager->getStorage('context')
              ->loadMultiple($ids);
    
    
    foreach ($itemsToDelete as $item) {
       $item->delete();
    }
    

    【讨论】:

      【解决方案4】:

      错误消息是因为您用于查询配置实体的类没有delete() 方法。对于内容实体也是如此。 delete()方法是实体实现的,所以正确的代码类似如下。

      $storage = \Drupal::entityTypeManager()->getStorage('context');
      $query = $storage->getQuery();
      
      $ids = $query->condition('user_id', $current_user)
        ->condition('key_text', $key)
        ->execute();
      
      foreach ($storage->loadMultiple($ids) as $entity) {
        $entity->delete();
      }
      

      见entity.query service deprecated in favor of EntityStorageInterface::getQuery()。

      【讨论】:

        【解决方案5】:

        我知道它很旧,但我遇到了这个并认为我应该发布..
        我有一个自定义实体表,我想从中删除项目。我不是 100% 确定此方法的含义,但我想 它不会调用任何实体挂钩 或任何东西,但它会提供更多性能。

        $database = \Drupal::service('database');
        $query = $database->delete('my_custom_entity_table');
        $query->condition('my_field', $value_to_check_for);
        $query->execute();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-04-04
          • 1970-01-01
          • 2019-04-21
          • 2011-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多