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

评论 (写第一个评论)