gpt4 book ai didi

entity - 在某些情况下删除实体 Drupal 8

转载 作者:行者123 更新时间:2023-12-02 04:10:45 25 4
gpt4 key购买 nike

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

$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();

但是代码返回: fatal error :调用未定义的方法 Drupal\Core\Config\Entity\Query\Query::delete()

最佳答案

要查询实体,您可以使用 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();
}

关于entity - 在某些情况下删除实体 Drupal 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36860462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com