使用 entityQuery 怎么基于引用字段的值进行条件查询?
发布于
使用 entityQuery 按条件查询普通字段时可以使用 condition()
进行查询,比如查询所有逾期的项目的代码如下:
$result = \Drupal::entityQuery('node')
->condition('type', 'project')
->condition('field_status', 'overdue')
->execute();
但如果要基于被引用的节点的字段进行条件查询的话要怎么写?比如任务(task
)节点通过所属项目(field_project
)字段引用了一个项目(project
)节点,如何查询所有项目状态为逾期的任务?
1 个回答
entityQuery 中对于引用字段可以通过 [引用字段].entity:[实体类型].[目标字段]
方式进行条件过滤,示例代码:
$nids = \Drupal::entityQuery('node')
->condition('type', 'task')
->condition('field_project.entity:node.field_status')
->execute();