6.1 实体查询和加载

龶龴龰龭龪龦 Drupal 8 龒龑龐龍龋龈龄龁

本课内容

  • 齾齽齺齸 examples/query_example
  • 齥齡龈龄龍龋
  • 齥齡龈龄齠齞
  • 齛齚齘齕齑 PageExampleController

Drupal 鼹鼸鼵鼱鼭鼬鼩鼦鼤鼣鼡鼠鼞龈龄鼛鼘鼦鼔鼐鼏鼍鼊鼈鼄鼀黿黼黸黶黵黳黲黮齺齸黬鼬鼩黨 Entity API 點龁Drupal 7 鼦鼱鼭鼬鼩黚 EntityFieldQuery 鼻鼄鼞黒黐黌黉鼦鼔黅黃麿龈龄龁麾麺鼻麹麸黚麴麺鼔鼐鼄麱黮麭黒麫麪麧麥鼻麡麞麛麙龈龄龍龋龁鼀麘麗麓麑麍麌龍麊 How to use EntityFieldQuery

Drupal 7 麆麂鼄 EntityFieldQuery 麁鼱鼭鹽鹹 entity.query 鼦鹴鹱鹭鹬鼄鹪鹧黐鹥鹤鹣鼦龈龄鼻鹟龈鹛鹘鹔麺龍龋鹑鹍龁 entity.query QueryInterface 鼦鹔麺龈鹇龁鹆鹧黐鹃鸿 \Drupal 鸷麫鸶鸳麥鸱鸭鸬鸨鸧鸣鼦鼠鼞麾麺鹴鹱龁

实体查询服务

麾麺龍龋鹴鹱鸢鸡鸟鸞鸜鸙鸗鸓鸑鸏齺黅鼦鹔麺鸌鸊鸆鸃龁麾鼻鸂鹔麺 SQL 龍龋鼄鷻鷺鹌鷹鹔鷵鷲鷮鼄鷫鹌鷧齑鹔麺龍龋鹑鹍鼄麆麂鷣麙鹪龁鸟鸞黶鷟鷝鼞麾麺龍龋鹑鹍鼦鷛鷚鷗齠鸿鷔麥龍龋鷐鷚龁

鼊鷍鸏鼔黅鷧齑龍龋鹑鹍龁

基于类的方法(推荐)

麆麂鷋鸌龒鸟鸞鷉鷅鷄鼀麘鹴鹱(services)鶶鶵龁龶鹛龒鼄鸟鸞龰鼠鼞鼻鶱鶭鼡鷛鷚鶪鸙鹔麺龍龋鹑鹍鼄鼠鼞麾麺龍龋鹑鹍鸜鸙鶦鶥齺黅鼦鸌鸊 ID 鸆鸃龁

// This function and the next are part of the dependency injection pattern. 
// We will explain those in future chapters.
public function __construct(QueryFactory $entity_query) {
  $this->entity_query = $entity_query;
}

public static function create(ContainerInterface $container) {
  return new static(
    // User the $container to get a query factory object. 
    // This object let's us create query objects.
    $container->get('entity.query')
  );
}

// This function shows how to
public function myCallback() {
  // Use the factory to create a query object for node entities.
  $query = $this->entity_query->get('node');

  // Add a filter (published).
  $query->condition('status', 1);

  // Run the query.
  $nids = $query->execute();
}

静态方法(不推荐)

鶣麘鷚鹭鶡鷺鷉鶞鶜龦 *.module 麑鶘鶶黚鼄龑鶕鶑鶞鶜龦麾鶏鼦鶍鹌鷺黶鼠鼞鹴鹱鶵鶋鼦龁鸧鸣鼔鼐鶊鶆鶄鼞黒齕齑鶃鶁龁麾鷍鸏鵾鵺鵸鹧黶黵黳鸧鸣鷝鼞 entity.query龁

鹥黚鸧鸣鵴鼠鼞 entity.query鼄鵳齠黐鵱鶶鶵鵰鵸鼦鹭鶡鵮

$query = \Drupal::entityQuery('node');

龑鶕黳鼠鼞鷺鶊鼦龈龄鼻鹟鼄鹬鵡 node龁龦 $query 鶱鶭鶏鵸鹧黐鵟鹣鵛龐鵙鵖龈龄鼻鹟鼦鵒鶋鸷黐鶪鸙鹤鹣龈龄鼻鹟鼦龍龋鹑鹍龁\Drupal 鸷麫鸶鸳鼦 entityQuery() 鸧鸣鼔鼐鹌鼠鼞 entity.query 鹴鹱鼦鹔麺鵑鵐龁

查询实体

鵱鵌鹌鹔麺鼠鼞龍龋鹑鹍龍龋鸌鸊龈龄鼦鹛鵉龁

麾麺麭龶龍龋鵇鵅鵂鸊鼦鵁鴽鸌鸊龁$nids 鶱鶭鵇鵅鸌鸊 ID 鼦鷚齾龁

/**
* @var $query \Drupal\Core\Entity\Query\QueryInterface
*/
$query = $this->entity_query->get('node')
  ->condition('status', 1);

$nids = $query->execute();

注意:

象 PHPStorm 这样的 IDE 里,注释有自动完成功能。在我们的例子里,使用了一个变量声明的注解, 说明这个变量是 QueryInterface 类型以及这个类型所在的位置。

查询条件

鴰黮鶶鶵鴯龒鼦鹛鵉鴭黚鼱龈鵁鴽鴬鸣鴨鴦鷉鼠鼞麡麞鼡麫麪鷵鶘龁麾麺龍龋鶏鼄鸟鸞鵇鵅鴣鼊麁鵁鴽鼦鸌鸊 ID鼄鵖鸞鼦鴠麂鼀鴜鴚鸳龦麾麺龍龋鴗麙麆鴓龁鸌鸊鸷鴒鴑麹麸鴏ipsum lorem鴎龁鸌鸊鼦 field_tags 麫麪鴊黒鹽鹹鴏test鴎鼦鴇鼻鴅鷲龁

龦 Drupal 7 鶏鼠鼞 EntityFieldQuery 鼦鴚鴄鼄 propertyCondition 鼡 fieldCondition 鹌鷺鶊鼦鼄鷻鹇龦 Drupal 8 鴁鼊鴯鴀龁Drupal 8 鼦 QueryInterface 鼬鼩鼦 condition() 鼔鼐鶥鳼黚麡麞鼡麫麪鷵鶘鳼鳹鸢鸡鷧齑鷵鶘齾龁麭黒龈龄鳷鼞鵟鹣鷵鶘鹆鹌鹧黐鼦鼄麾黳鹃鸿麫麪鸷鷗齠 entity.(column) 黬鵟鹣鷵鶘鼄鹍鼵鵌鳵麺鴇鼻鴅鷲鳵鶆龁

$query = $this->entity_query->get('node')
    ->condition('status', 1)
    ->condition('changed', REQUEST_TIME, '<')
    ->condition('title', 'ipsum lorem', 'CONTAINS')
    ->condition('field_tags.entity.name', 'test');

$nids = $query->execute();

查询条件分组

麾麺鹛鵉鳴鳱鳭鶆龦鳫鳨龍龋鶏鷧齑 AND 麥 OR 鷵鶘齾龁鸟鸞鳜鶱麆鴓鼦龍龋鼄鵇鵅鳚鳖鵱鵌鵛鳔鹔麺鷵鶘鼦鸌鸊鵮

  • 鳐鳍麹麸鴏ipsum lorem鴎
  • field_tags 麫麪鳷鼞鵰鴏test鴎鴇鼻鴅鷲

麾麺 OR 鷵鶘齾鼠鼞 orConditionGroup 鼔鼐鷧齑鳼鼣鹥鷵鶘鳈鳄鳂龍龋鹑鹍鼄鵸鹆鹧黐鼠鼞 andConditionGroup 鷧齑 AND 鷵鶘齾龁

$query = $this->entity_query->get('node')
    ->condition('status', 1)
    ->condition('changed', REQUEST_TIME, '<');

$group = $query->orConditionGroup()
    ->condition('title', 'ipsum lorem', 'CONTAINS')
    ->condition('field_tags.entity.name', 'test');

$nids = $query->condition($group)->execute();

查询修订版本

鳁鲿鼄龈龄龍龋鲾鵇鵅鲽麺龈龄鴠鴜鼦鲹鲸鲵龶鼄鵸鹧黐鼠鼞 allRevisions() 鶪鸙鴣鼊鲹鲸鲵龶龁

$query->allRevisions();

评论 (写第一个评论)