我有一个Drupal 8内容实体,与一个允许多个值的分类术语相关。
我想查询实体并获取仅包含我在查询中使用的术语的内容。
问题是我找不到用多个分类术语查询我的实体并获取与它们关联的内容的方法。

我的内容实体(节点捆绑包)称为“鸡尾酒”,除其他外,还有一个实体参考字段,称为“ field_ingredients”,该字段与分类词汇“成分”相关。
我想获得一个实体,该实体具有例如2种具有分类ID的成分:40 AND35。我尝试了以下代码,但未成功:

$query = \Drupal::entityQuery('node');
$query->condition('type', 'cocktails');
$query->condition('field_ingredients.entity.tid', 40);
$query->condition('field_ingredients.entity.tid', 35);
$node_ids = $query->execute();


$ node_ids为0

还有这个:

$query = \Drupal::entityQuery('node');
$query->condition('type', 'cocktails');
$query->condition('field_ingredients.entity.tid', array(40, 35), 'IN');
$node_ids = $query->execute();


$ node_ids为3,它返回具有两个分类法ID(或逻辑)之一的节点ID,

正确的答案应该是一个节点ID,这是与两个分类法ID(包含两种成分的混合物)相关的节点

最佳答案

最后,该解决方案作为评论发布在实际的Drupal API文档中。

$query->condition($query->andConditionGroup()->condition('field_ingredients', array(40, 35)));


请查看以下链接以获取更多详细信息:

https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Query!QueryInterface.php/function/QueryInterface%3A%3AandConditionGroup/8.2.x

关于第一个注释和第一个示例(“工作代码”)

09-16 01:31
查看更多