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

评论 (写第一个评论)