60.实体查询entityQuery

龵龴龱龰龮龪龨龦龥龣龢龞龚龖龕龓龒龑龍龋龈龇龅龂龀齽齼齹齸龣齶齴齰齮齬齸齹龑龍龣龈龇龅齨齦齣齢齠齞齶齝齜齨齘齗齕齨齶齗齕齨齑齍齌龱齨齶齈齅齍齂龱齨鼾龈龇齨鼾齂龱龈龇齨齶鼺鼹齠鼶龈鼲鼮鼪鼨鼦鼤鼣龣龑龍鼟齸齹龋鼶鼜齨龂齶鼘齽鼔鼐龋鼌鼈鼅鼄鼨鼦龦龥齞鼁齶黽龣龈龇黻黷齹齸龋黴鼜齨龂齶黱黭齮鼨鼦黪黩黨黥黢點黛默黕黓黏黍黊黇黃黀麿龣龑龍麻齶齽麺麷麶麴麲黇麯麫麧麥麣麟麛龋麗鼨麔麐龣麺麷齶齽麎麍麊麇齜龈龇龅麃麿鹿鹽鹹鹷齶鹶鹵黕鹳龱齮鹯鹫鹨鹧龣龑龍鹣鹟鹝鹚鹖齸齹鹓龨鹫鹒齶默黕龓龒齸齹鹑鹍齘鹉龍龣齨龮麶鹆齶鹂鹂鹁黕鸿鼶鼦鹑鸼 JOIN 鹹鹷鸱鸰齶黱龀鸭鸪龣齶鸩鼘齽齍鸧鸦鸢龀鸠齜齞鼁龣齶鸞齽黕鹹鹷黍黊麗鼦黀麿龣齜鸛點鸘龣鸕鸔齶黱鸒鼲鼮鸏鸌鹖鼶鼦龑龍齞鼁齶鸉鸇鸃鹿鼨鸀鷼齠齶鷸鷶龋龰鷵龂麫鷳龑龍龣鹹鹷鷰鷯齈鷬齶鷩鷨龰鷵齣鼔鼐鷧鷤鷢鷵鼨龣龑龍鹹鹷鷞鷜齶鷛齣龑龍鹹鹷 Entity query 齶黽鼅鼄龋龈龇龅麃麿鶻龑龍齸齹鼹鶸齶鶴鶰鷢龦鶬鶩鶥齶齜鸛鶢默黕齴齰鶟麊鶜鶚齜鶙齈齶鶗齑麫鶕齸齹鹑鹍齘鸉鸇龣鹹鹷鹍鶒齶齍齢鸒齞鼁龣龑龍鹹鹷黨

鶏鶌龑龍鹹鹷 Query

鸩齈鷛齣齌龱龑龍鹹鹷齶齈鶆鶃龇龑龍鶀鵼鵹鶙龑龍鸦鸢鵷鹧鵶鶗鵴龣鵲鵱鵭黀麿齶黓鹖黍黊黀麿龣龑龍齶齽鼨鼦龑龍齍麟麛鶀鵼齶齈鶆龵龴鵱鵭龋麗麟麛麔麐黱鼦黀麿黓鹖黇麯龣龑龍齶鶏鶌龑龍鹹鹷齈鵩鵥黍黊黀麿龣龑龍 id 鵟龑龍龈鵞鵛

龑龍鵗鵖鹹鹷 QueryAggregate

鵗鵖 aggregate 鸩齈鵍鵊齣鵗鵆鼾鵆鵄鼾鵀鼺鵹龋鴼鼶鴺鴷龂鶚齜鵗鵖鼨麥齶鷩鷨龱龰龮鷛齣鵗鵖鵶齶鴴鷳 sql 鸱鸰龣鵗鵖鴬龈齶龋 sql 龂鶏齜龣鵗鵖鴬龈齍黨 鴫鴧鴥鵲鼾鴫鹽鼺龈鼾鴤齠鵲鼾鴤鴢鵲鼾鴫齘齶龋 sql 龂黱鸧鴬龈鴟齜鷳鹹鹷鹑麎鵖齶龋龑龍鹹鹷龂鴞鵷龑龍鵖鹹鹷齶齈鹿鹽齽麗麺麷龣鴧鴥麟麛鼹齞龣鹹鹷齶鵗鵖鹹鹷龀鼅鼄龋鶏鶌鹹鹷鼹鶸龣齶鷩鷨龑龍鵗鵖鹹鹷鴝鴙鴗鶏鶌龑龍鹹鹷鵛

龈龇龅麃麿龓鴕鵥鴓黨

黱鴐鴎鼨鼦齴齰龣鵥鴓齘鴊鴈齶鴇黃鴆鴂默黕齜鹖龣龓鴕齶龚齈鶆鳿鳽龱龰龮龈龇龅鳻黩鷢鴂鳹鼶齶齽麎龚鵷龈龇龅鳸鳶鹁鶗黊齶齌龱龣齽鶗鳳鳰齦麃鹹鹷鼾鵗鵖鴬龈鼾齨鳬鷜鸦鸢鼾鹒麶鼾鸼鹑鹆鹆鼅鳪龚鳧鳣鳠齌鳝齶龋鴊鴈齬鳚鴐龨鳖鳔鳐齠鳎鳋鳉鷢鼨鸧龢龞鴺鴷鵛

鳆鹖鼨鼦龈龇龅鳃麊齈鶆鶚齜鶆鼪鷞鳁黨

    $con=\Drupal::database(); //获取配置中$databases['default']['default']表示的链接,这对于大多数只有一个数据库的站点而言是最常用的,全局获取  
    $con=\Drupal::service("database"); //完全等同于\Drupal::database();  
    $con=$container()->get("database"); //效果同上,在容器对象可用时使用  
    $con=\Drupal::service("database.replica"); //获取配置中$databases['default']['replica']表示的备用数据库链接,无设置将回退到主库  
    $con=$container()->get("database.replica"); //效果同上,在容器对象可用时使用  

鶆鶸鷞鳁鵩鵥鼨鼦鳃麊鵷鹧齶黽鶴鶰鷢鶏齜龣龈龇龅鱧鴟麊鶜齶鱥龂黨

$con->select($table, $alias = NULL, array $options = [])

鵩鵥鼨鼦鹹鹷鵷鹧齶龱鳻黩黕鴆龣龑龍鹹鹷鶚齜鱚鵷鹧鱗鹽龈龇龅鹹鹷

龈龇龅麃麿鷧鷤龣鹹鹷鵷鹧黨

齜鷳鹍鶒鼨黀 sql 鹹鹷鸱鸰齶黽鷧鷤鴼鼶鷞鳁鱖鼔鼐齈鶆鱧鴟鸱鸰龂龣鶗鵴鶬齦齶齈鶆鹍鶒鱕鱑龣 select 鸱鸰齶龋鴤鱏鱗鹽麻黽齈鶆鱍龇鱊鷞鳁鷧鷤龣龈龇鴗鱆麃鱄麺鱁鼨黀 sql 鸱鸰齶鷸鼌鶚齜龪鸕鷧鹖龣鳃麊鵷鹧龣 query 鷞鳁鹿鹽鹹鹷齶鹹鹷鵷鹧鷬黨

\Drupal\Core\Database\Query\Select mysql 鰷鰴鼤鲈鶚齜鱚齞齶鱥鰰龈龇龅齈齅齜鰬齞鴂鰫鷞鲊黪黩鵶

鹹鹷鼌鵩鵥鼨鼦 Statement 鵷鹧鷤鼔鼐鱧鴟鹹鹷鹑麎龈龇齶鱚 Statement 鵷鹧鷬黨

\Drupal\Core\Database\Statement

龋鹹鹷龪龰鷵鰪點鰧鰬齶鰥鰣鱚鹹鹷鵷鹧鳐鰢鰡齶鶆鰞鱊鰢鰡齍鰝鰚鰖鰒黽鵟黛鱗鹽鼨鸧鱧鴟鵛齣鷢鰑點黇麯龣鰧鰬齘鰥鰣鰏鰎龈龇齶鹹鹷鵷鹧龑鰌鷢鶆鼪麊鶜黨

\Drupal\Core\Database\Query\AlterableInterface

黽鶚齜鷢鹹鹷鰊鰆齘鹹鹷鴴龈龇齶鷬鼪鵛

鹹鹷鰊鰆 Tags

齜鷳鰊鴕鼨黀鹹鹷鵹鹹鹷鵷鹧鵶齶鯾鷶鱗鹽黇麯龣鰢鰡鰧鰬鵹龑鯺鶸鰊鰆鰫麯鷢鰪點龣鰧鰬麶齶鷬鼪鸕鵶齶鼨鼦鹹鹷齈鶆齍鱕鱑龈鵞龣鹹鹷鰊鰆齶齈鶆齜鹹鹷鵷鹧龣 addTag($tag) 鷞鳁鯷鷼齶鹹鹷鰊鰆鯶麶鶌鯵齣黨鸦鯲鼾龈鸦鶻鼪鯰鯬麃鯫齶鲈鶬鴢鸿鯧鶆鸦鯲黢鯦

鹹鹷鴴龈龇 MetaData

鱗鹽鹹鹷鯤麫龣鰧鰬鴬龈麻齶鯠鰥鰣鷢鹹鹷鵷鹧齶鯜鹹鹷鵷鹧齈鶆鯛黭鱕鱑龈鵞龣鰏鰎龈龇鳐鰢鰡鰧鰬齶龵龴鹹鹷鵷鹧龣 addMetaData($key, $object) 鷞鳁鯷鷼齶 $object 龀鯕鷼鰥鰣龣鯓鵞齶齈鶆龀鱕鱑 php 鯓鵞齶 $key 龀鼨鼦鸦鯏鯌鵲齶龀鼟鰥鰣鯓鵞龣鴕鹒鰊鯈齶鯶麶鶌鯵齘 php 龣鯓鵞鯶麶鶌鯵鯤鵴齶鰢鰡鰧鰬龵龴鯅鲢鵥鯕鷼鯓鵞

鹹鹷鰖鰒鰧鰬黨

龋鹹鹷鵷鹧鱗鹽鯂鯀龣龈龇龅鹹鹷龪鰚鱗鹽鱊鰢鰡龣鹹鹷鰖鰒鰧鰬黨

\Drupal::moduleHandler()->alter($hooks, $query);

$query 齣鹹鹷鵷鹧齶 $hooks 齣鼨鼦鰖鰒鰧鰬鹍鯫龣龈麃齶鱊鰖鰒鰧鰬龣麶鸦麃鯫齣黨 'query_' . $tag 齶鱊鰢鰡齽麎默黕鱗鹽齘鹹鹷鵷鹧齍麫龣鮹鮸齈鶆鴞鵷鱥鶗鵴鰊鰆鹿鹽鰧鰬龑鰌齶齽麎鴞鵷鮵齍鹹鹷鹿鹽鰧鰬龑鰌齶鹶鹵鰧鰬麶齣 'query' 齶鹉龍鰧鰬龣鴬龈麶齽鼪黨

鸞鵱鰊鰆齣 node_access 齶鰢鰡麶齣 yunke 齶鹶鹵鰧鰬鴬龈麶齣黨 yunke_query_node_access_alter

鴞鵷鮵齍鹹鹷龣鰧鰬麶齣黨 yunke_query_alter

龋鵥鴓鼤齑黕龣龈龇龅麃麿龓鴕鼌齶齮鮲鼨鮲龑龍鹹鹷齶黱鼹龪鷧鮯龚鵥鴓鯧鴈齦鮬鴂 SQL join 齜鳁齶黱龀龑龍鹹鹷龣鮫鮧龓鴕黃齶鯾鮦鮢鹫鹒鮲龑龍鹹鹷龣鮞龣鮛龀黕鹍鼅黏鸼鹑鼶鼦齨鯧鵱鵭黀麿龣鹹鹷鸱鸰

龑龍鹹鹷 entityQuery

龑龍鹹鹷鶚齜鼨鼦龑龍鹹鹷鵷鹧鮚鱗鹽鹹鹷鯤麫鮘齅齶龑龍鹹鹷齘龑龍齸齹鮔鮔鯤麫齶鼌黛齣龪黛鷧鷤齸齹鹑鹍齶鷩鷨龑龍鹹鹷鮒龓龒齽齼鹍鼅 SQL 鸱鸰齶龑龍鹹鹷鵷鹧龀鯾龑龍齸齹鷯鮬鱵龂鵩鵥龣黨

$entityTypeManager->getStorage($entity_type)->getQuery($conjunction);
$entityTypeManager->getStorage($entity_type)->getAggregateQuery($conjunction);

龰鷵鷧鷤鷢鶆鼪鮎鮋鲤鲢鷞鳁黨

\Drupal::entityQuery($entity_type, $conjunction = 'AND');
\Drupal::entityQueryAggregate($entity_type, $conjunction = 'AND');

鱊龑龍齞鼁龋龑龍齸齹鷯鮬鱵龣 getQueryServiceName 鷞鳁龂鵩鵥鴗鮊齞鼁默黕龣龑龍鹹鹷鮈鮆鮃鮂鵹龥鱵鮃鮂 id 鵶齶魿鱚鮈鮆鮃鮂麺鱁龑龍鹹鹷鵷鹧齶黱鴐鶆鶥黃龑龍齣龮齶黽龣鹹鹷鮈鮆鮃鮂齽鼪黨

鮃鮂 id entity.query.sql

齞黨 Drupal\Core\Entity\Query\Sql\QueryFactory

鱚鮈鮆鮃鮂鶗鯠齣鶥黃龑龍鮵齜齶鹁齈鶆齣鱊鸒龑龍齞鼁麺鱁齜鷳 sql 龣龑龍鹹鹷鵷鹧齶黽鵩鵥龣鹹鹷鵷鹧龀龰鷵鷧鷤龣鰷鰴龑龍鹹鹷鮃鮂齶鷬鼪齶鼔鼐魺鱚鼺龀龵龴龑龍鹹鹷鮈鮆鮚龑魷魵龑龍鹹鹷鵷鹧齶鷶鶗龀麍麊龑魷魵鵛

鰷鰴鶏鶌龑龍鹹鹷鵷鹧黨

齞黨 \Drupal\Core\Entity\Query\Sql\Query

龑鰌麊鶜黨 \Drupal\Core\Entity\Query\QueryInterface

鮵齍龣龑龍鹹鹷魳魺鱚龑鰌鱚麊鶜齶鴐龨魯魬鷢龑龍鹹鹷鵷鹧鷧鷤龣鮘齅鷞鳁齶黕龓龒魪鹵鶚齜龑龍鹹鹷鮲鱚鷞鳁鶙齈齶鱚麊鶜鴝鴙鷢鶆鼪麊鶜黨

\Drupal\Core\Database\Query\AlterableInterface

黱鮛齣鰧鰬鰪點鳣鼪鷢齌鳝齶龑龍鹹鹷鵷鹧龦鶬麴鶰魧龈龇龅鹹鹷鵷鹧鵛

龑龍鹹鹷龣鮵齍鷞鳁龚齈鶆鹹鮲龪鸕鷧鹖龣麊鶜鸕魤齶黱鴐鼔鼐鴇黃鴆鴂鶆鼪鷞鳁龣鶚齜黨

$entity_query->condition($property, $value = NULL, $operator = NULL, $langcode = NULL)

鱚鷞鳁龂 $property 齣黀麿鶀鵼麶齶魢鮛龀黕鴞鵷鱥鹿鹽黀麿魟鹹龣鸦鸢鵷鹧鶀鵼麶齶黽魢齈鶆龀魛鰎鼨鼦黀麿鵷鹧鵹鷬鼪鸕龣黀麿麃鵶鵛

黀麿鶀鵼麶黨

黀麿龂鶚齜龣鶀鵼麶麇齜鸦鸢鵷鹧鶻鱥鶀鵼麶齶鷶鶗龀龈龇龅龂龣齨龮麶鵹鶚齜麻鼔鼐鶗默黕龓龒鹶鹵鶩鶥龣魚鼲齶龑龍龀鹯鹫鹨鹧齶鶗默黕麫鶕齸齹鶩鶥鵶齶齈鶆龀齽鼪魖鸒黨

鸦鸢麶 + . + 鶀鵼麶

鶀鵼麶龀麯魊龋鸦鸢鵷鹧龣 schema 鷞鳁鴐龨龣龮麶鸦齶鷶鶗龀龈龇龅龮麶齶龈龇龅龮麶齈齅龀龋鶀鵼麶龪鷼鷢龪魇龣

鯠鯠鼨鼦鸦鸢麶

齽麎龀齌龱鸦鸢鵹魢鮛龀齸齹龋齗齕齨鴐龨龣鸦鸢齶鮞龪鯠齣齰鶀鵼鸦鸢鵶鵟鶀鵼麶齣鳻鶀鵼麶齶鹶鹵鶀鵼麶齈鶆魅魃齶鷩鷨齈鶆齴鸿齣鯠鼨鼦鸦鸢麶

鸦鸢麶 + . + 魏鼪鰊魌 + . + 鶀鵼麶

齈齜鼪鰊魀鬽鬻鬸鼶鵲鸦鸢鵹鶕鱑鼶鵲齘鼶鶀鵼龣鬴鹒鵶齶鼪鰊齈鶆龀鼨鼦鹉龍龣龈鸦齶齨鲒鶢鬻鬸鱚鼪鰊鵲龂鶀鵼麶黍黊黀麿龣龑龍齶鶕鱑龋鼶鵲鸦鸢龂鬳鼨鼦鵲鼪鰊齣 0 鬬鼪鰊魢齈鶆齜魏 %delta 魌鬩鮦鱕鱑鼪鰊鵲齶黱鸒鼲鼮鹆鱸鷳鸦鸢麶鷼鶀鵼麶齶鷩鷨齈鶆魅魃

鸦鸢麶 + . + 魏鼪鰊魌

齽麎鼪鰊齣龈鸦齶鹆鱸鷳鸦鸢麶 + . + 魏鼪鰊魌 + 魏鸦鸢鳻鶀鵼麶魌

齽麎鼪鰊齣魏 %delta 魌齶鹶鹵黀麿鱧鴟龣龀鼪鰊鶀鵼鵲齶鯤黥鷳鶆鶸鬳鼨鸒鼲鼮龂鶀鵼麶鹆鷳魏 delta 魌齶鯜龀鶗齅鸿齣 fieldname.delta 齶鷩齣 delta 龀鼨鼦黇鬨龣鶀鵼齶鯧鬥龋鸦鸢麯魊龂鬩黏齶鯠齹龋鷳龈龇龅龑鰌鹓龨

鸦鸢麶 + . + 魏鸠齜齞鼁魌 + : 龑龍齞鼁 id + . + 鸦鸢麶

黱齜鷳鸠齜齞鼁鸦鸢龣鬻鬸齶鮘齅鬣齠齶齈鹿鹽鬟龑龍齞鼁鬻鬸齶齽龋鶥黃龑龍龂 uid.entity:user.name 齨鲒 uid 齸齹龣龣齜鸛龣齜鸛麶齶黱麫鸼鹖鷢齜鸛龑龍鬬黱鸒鰢鷜鼪鬞鳧鸦鸢麶鮦齨龣齑鬜龀鼨鼦鸠齜齞鼁龣鸦鸢齶魏鸠齜齞鼁魌龵鶏龀魏 entity 魌齶齨鲒鸠齜鹖魛鰎鼨鼦龑龍齶鷨鰊鴕鯏鼟麯魊龋黨

\Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem propertyDefinitions 鷞鳁龂

: 龑龍齞鼁 id 魌鶬齦鬩鲒鸠齜鹖龣龑龍齞鼁齶齈鶆魅魃齶鷩鷨龪魷鹆鵴鷳 uid.entity.name 齶鯾鬓黷黏鬒龣鬎鸃鮲鳖鳔鼅鳪魅魃齶黱鬍龰鷵鰚鱍龇鸦鸢麯魊鴗鱆鵄鬋鸠齜鹖龣龑龍齞鼁齶鯾鵼齅鬎鸃鮲齈鶆鶗魅魃齶鯜魢鮎鶗鷢鼶黷齶鱚鸒鰢鷜鼪鬊鬉龣魏鸦鸢麶 魌鮛龀鬈龑龍齞鼁鶸龣鸦鸢麶鷢齶麣齈鶆鴇鸉鱚鰢鷜鶆鹿鹽鳹鷼鸉鸇龣鬻鬸鵛

黀麿鵲黨

黀麿鱧鴟龣鵲龵鶏龀鼨鼦鰊鵞鵲齶鵟黛龈麃齶龈麃龂龣鬄鬁麲魊鶃髾鷳鱧鴟鯏鬬齽麎鵲龀鸦鯏鯌鯵龀髽齠鴢鸿髼髺鲢鰫鷳鸦鸢鶀鵼麯魊龂龣 case_sensitive 髷鵱鵭黨

$fieldStorageDefinition->getPropertyDefinitions()->getSetting('case_sensitive');

黀麿鱧鴟鯏黨

龑龍鹹鹷龣黀麿鱧鴟鯏齈鶆鶚齜龈龇龅麃麿鷧鷤龣鮵齍鱧鴟鯏齶龋鷨齌鳝鶸齣鷢鷞鰞鶚齜鹿鼨鸀龣鶴鶰鷢鼨鸧鹯鹫鱧鴟鯏髳齶齽鼪黨

'<>' 黨鶗鹆鷳齶齠鴢鸿鶗髼髺麻龦鶬鼟髮魵齣 NOT LIKE

'STARTS_WITH' 黨鶆髨鹵黢髤齶龵鶏齜鷳鸦鯏鯌鬻鬸齶龦鶬鼟髮魵齣 LIKE value%

'CONTAINS' 黨麴麲髨鹵齶龦鶬鼟髮魵齣 LIKE %value%

'ENDS_WITH' 黨鶆髨鹵鹑髣齶龦鶬鼟髮魵齣 LIKE %value

黥鬻鬸龣鵲齠鴢鸿髼髺麻齶 LIKE 鶬齦鶚齜 LIKE BINARY

鶕鱑黀麿鱧鴟鯏龱髛龀齠鴢鸿鶗髼髺龣齶齽魏 in 魌魢齈鶆龀魏 IN In iN 魌齶龋鱗鹽鸱鸰鶸鰚鼟鷵鼨齣齠鸿

鸱鲊鮦鮢黨

鬩鲒鬻鬸鼟髚麯龋麗鼨鸒鸱鲊鶸齶齽麎龀鮵齍鸱鲊齶鹶鹵魅魃鱚髙龈齶黱鴟齜龋 JOIN ON 黀麿鶸齶魢鮛龀鳧龵龴鸱鲊鮦鮢龴髖龈龇髒鹿鹽鸼鹑

鹹鹷髐魷黨

髐鼨鼦龮鰬齮齬鳚齶黱鴐鸞鵱鹳鶥黃龑龍龣 body 鸦鸢鵱鵭齣鼶鵲鸦鸢齶鱗鹽鼪龨龣鮦鮢黨

$entity_query=\Drupal::entityQuery("node", 'AND');
$entity_query->condition("title", [520], 'IN');
//查询标题为“520”的节点
$entity_query->condition("body.2.summary", "yunke", '=');
//查询body字段下标为2的值(第三个值)中摘要为“yunke”的节点
$entity_query->condition("body.%delta.summary", "yunke", '=');
//查询body字段任意下标中摘要为“yunke”的节点,这其实包含body.2.summary,也等效body.summary,所以我们在进行实体查询时需要注意优化
$entity_query->condition("body.3", 5, 'IN');
//查询body下标为3的值中主属性等于5的节点,body的主属性为“value”,等效于body.3.value
$entity_query->condition("body.%delta", 5, 'IN');
//查询body字段下标值为5的节点,条件作用在列名delta上
$entity_query->allRevisions();
//对全部版本进行查询
$ids=$entity_query->execute();

麺鱁龣鹹鹷鸱鸰齽鼪黨

SELECT base_table.vid AS vid, base_table.nid AS nid
FROM 
{node_revision} base_table
INNER JOIN {node_field_revision} node_field_revision 
ON node_field_revision.vid = base_table.vid
INNER JOIN {node_revision__body} node_revision__body 
ON node_revision__body.revision_id = base_table.vid AND node_revision__body.delta = :delta0
INNER JOIN {node_revision__body} node_revision__body_2 
ON node_revision__body_2.revision_id = base_table.vid
INNER JOIN {node_revision__body} node_revision__body_3 
ON node_revision__body_3.revision_id = base_table.vid AND node_revision__body_3.delta = :delta1
WHERE 
(node_field_revision.title IN (:db_condition_placeholder_2)) 
AND (node_revision__body.body_summary = :db_condition_placeholder_3) 
AND (node_revision__body_2.body_summary = :db_condition_placeholder_4) 
AND (node_revision__body_3.body_value IN (:db_condition_placeholder_5)) 
AND (node_revision__body_2.delta IN (:db_condition_placeholder_6))

髒齮鮲鼨鼪鸠齜齞鼁鸦鸢龣鬻鬸黨

$entity_query=\Drupal::entityQuery("node", 'or');
$entity_query->condition("uid.entity.name", "yunke", '=');
//找出作者为“yunke”的所有节点实体
//等同于:$entity_query->condition("uid.entity:user.name", "yunke", '=');
$ids=$entity_query->execute();

麺鱁龣鹹鹷鸱鸰齽鼪黨

SELECT base_table.vid AS vid, base_table.nid AS nid
FROM 
{node} base_table
LEFT JOIN {node_field_data} node_field_data ON node_field_data.nid = base_table.nid
LEFT OUTER JOIN {users} users ON users.uid = node_field_data.uid
LEFT JOIN {users_field_data} users_field_data ON users_field_data.uid = users.uid
WHERE users_field_data.name LIKE :db_condition_placeholder_0 ESCAPE '\\'

鹹鹷鵩鵥鵲 :

齽麎龀鵄龈鹹鹷鵹骮齜鷢 count() 鵶鯵鵩鵥鼨鼦鼣龈齶鮦齨鯏鵆黀麿龣龑龍鼺龈齶齽麎龀骬鵄龈鹹鹷鵩鵥鼨鼦龈麃齶龈麃龣麧麶龀齂龱 id 齶麧鵲齣龑龍 id 齶齽鼪黨

        $entity_query=\Drupal::entityQuery("node", 'or');
        $entity_query->allRevisions();
        $entity_query->count();
        $arr=$entity_query->execute();
        print_r($arr);
        exit();

鶆鶸鮦鮢骩黏鼨鼦鼣龈齶齽麎骧齍骮齜 $entity_query->count(); 鹶鹵骩黏齞骥齽鼪黨

Array
(
    [36] => 9
    [47] => 9
    [48] => 9
    [37] => 10
    [38] => 11
    [39] => 12
    [40] => 13
    [41] => 14
    [42] => 15
    [43] => 16
)

齍鷢龑龍 id 鵟齂龱 id 鮛齈鶆鶚齜龑龍齸齹鷯鮬鱵鼨鶥鴆龣鷞鳁鵷龑龍鹿鹽鱧鴟鷢

鹹鹷黀麿麃黨

鼶鼦黀麿龵龴鳃麊麥鹍鯫鼨鼦黀麿麃齶龋鵴鼨鼦黀麿麃龂鳃麊麥龀鼨鬍龣齶黕鹵齣 AND 齶黕鹵齣 OR 齶鯜鼔鼐齈鶆鹳鼶鼦黀麿麃髒骟齜鳃麊麥鳃麊鹍鯫鼨鼦鳹齠龣黀麿麃齶黀麿麃龣骛骘齈鶆龀鼶鹓龣齶黱鬍鼔鼐鮛齈鶆龑鰌魏麣魌齘魏鵟魌龣骕齜齶麫鷳黱鼨黃龋鴊鴈齬鳚龂齍鳹鹿鼨鸀龣鴂骑齶龑龍鹹鹷魢齈鶆鶚齜黀麿麃齶鼘齽黕鹿鹽黱鬍龣鹹鹷黨

黓黏鴟黛齣魏 yunke 魌鵟魏 admin 魌龣齜鸛點鸘龣鸕鸔齶麣點鸘骍骉齣點鸘

齈鶆鶚齜鶆鼪鮦鮢鹿鹽黨

        $query = \Drupal::entityQuery('node', "AND");
        $group = $query->orConditionGroup()
            ->condition('uid.entity:user.name', 'admin')
            ->condition('uid.entity:user.name', 'yunke');
        $query->condition($group);
        $group = $query->andConditionGroup()
            ->condition('status', '1')
            ->condition('type', 'article');
        $query->condition($group);
        $entity_ids = $query->execute();

鹹鹷髅魵黨

龋龑龍鹹鹷龂齶骅鯷鷼鼨鼦黀麿齶龋鴤鱏鱗鹽龣 SQL 鸱鸰魳鰚鯷鷼鼨鼦鸼鹑鸱鸰齶骄骃龀鵴鼨鼦齨齶魢鰚齜鶗鵴龣鹒麶鹿鹽鼶骟鸼鹑齶黱龋龈龇龅龂默黕鹿鹽鸉鸇龣麥鳁髅魵齶龋齍鸧龈龇龅鶸骁黻鵷鹑鸼骟龈齍髚鬽齶鮵鶆龋鵱鵄黀麿鸱鸰龣麻驽默黕鶕鱑鱚黪黩齶驼鵞鬓黷鸼鹑骟龈

鰷鰴龑龍鵗鵖鹹鹷鵷鹧黨

齞黨 \Drupal\Core\Entity\Query\Sql\QueryAggregate

龑鰌麊鶜黨 \Drupal\Core\Entity\Query\QueryAggregateInterface

鵗鵖鹹鹷龀鵷麗鸦鸢鶀鵼鱗鹽黱鸧 SQL 鴬龈 SUM AVG MIN MAX COUNT 齶黽鴝鴙鴗齌龱龑龍鹹鹷齶龚默黕鳧鴈齦鮬鴂 SQL 龣鵗鵖鴬龈鼾齦麃鹹鹷 GROUP BY 鼾齦麃黀麿 HAVING 鹆鹆

黕龓龒黽龣鶚齜齶齜鼨鼦龮鰬龀麍驴龣黨

鸞鵱龋鼔鼐龣龰鷵龂齍齢鼦龦龥齞鼁驱驭魏 pen 魌齘马齨魏 watches 魌齶鰰鼐齗齜鼨鼦麟麛鸦鸢魏 price 魌齶鰌龋黕黓黏鴤鰞驧龣驱驭齘马齨龣麟麛齶齈鶆齜齽鼪鮦鮢黨

        $entity_query = \Drupal::entityQueryAggregate("node");
        $entity_query->aggregate("field_price", "MIN");
        $entity_query->groupBy("type");
        $entity_query->condition("type", ["pen","watches"], 'IN');
        $arr = $entity_query->execute();
        print_r($arr);
        exit();

骩黏黨

Array
(
    [0] => Array
        (
            [type] => pen
            [field_price_min] => 1038
        )

    [1] => Array
        (
            [type] => watches
            [field_price_min] => 260
        )

)

鱗鹽龣 sql 鸱鸰齣黨

SELECT node_field_data_2.type AS type, min(node__field_price.field_price_value) AS field_price_min
FROM 
{node} base_table
LEFT JOIN {node__field_price} node__field_price ON node__field_price.entity_id = base_table.nid
INNER JOIN {node_field_data} node_field_data ON node_field_data.nid = base_table.nid
LEFT JOIN {node_field_data} node_field_data_2 ON node_field_data_2.nid = base_table.nid
WHERE node_field_data.type IN (:db_condition_placeholder_0, :db_condition_placeholder_1)
GROUP BY node_field_data_2.type

鳹鼶鷞鳁龣鶚齜驦鮲麊鶜龂龣鶕骑

鴗麯魊龑龍鹹鹷黨

龋鰷鰴龣鹹鹷鮈鮆鵹鮃鮂 id entity.query.sql 鵶龂齈鶆鮲鹖鼔鼐齈鶆齽齼鴗麯魊鴗鮊龣鹹鹷鵷鹧黨

1 鼾麯魊鼨鼦鹹鹷鮈鮆鮃鮂齶鴝鴙龰鷵鰷鰴鮈鮆鮃鮂

2 鼾龋齸齹鷯鮬鱵龣 getQueryServiceName 鷞鳁龂鬩麯鬳鼨鸀麯魊龣鮃鮂

3 鼾龋鴗麯魊龣鮈鮆鮃鮂鵴鹫鮞驤麯魊鹹鹷鵷鹧鵹齞麶黨 Query 鵶鶻鵗鵆鹹鹷鵷鹧鵹 QueryAggregate 鵶鶙齈鵹鸕麿麶驡齞麶鯤鵴齶齘鴗麯魊龣鮈鮆鮃鮂龋鯤鵴麶鸦驝麐鼪鵶

鴗麯魊龣鹹鹷鮃鮂鹂鹂齈鶆鴝鴙龰鷵鰷鰴鷧鷤龣鹹鹷鮃鮂黨

\Drupal\Core\Entity\Query\Sql\Query

\Drupal\Core\Entity\Query\Sql\QueryAggregate

鯧龋鷨齌鳝鶸鹿鹽默黕龣鰖鰒

龑龍鹹鹷龋齸齹鷯鮬鱵龂龣齴齰鶚齜黨

鮲鼤龑龍鹹鹷鼌鰌龋龚魺鱚齅驛龥驗龣鮬鴂齸齹鷯鮬鱵龂龣鶆鼪鷞鳁鷢黨

鱍龇龑龍龣鶀鵼鵲鷼驓龑龍黨

public function loadByProperties(array $values = []);
public function getQuery($conjunction = 'AND');
public function getAggregateQuery($conjunction = 'AND');

鴊鴈齬鳚黨

1 鼾龋驐驍齞驊驉龂鶆鳖鳔龣龕驆齮鮲驃鴇鯾魚黢點鮈鴟龣鴟黛鼘鯾魚驁騿鮈鴟龣鴟黛鴆龣鳹鷼騼騸齶鶗鰚騵鶸騱騮騬魃麫麧黃齶齈齅黏鰌龣騪黃鹂鹂魢齅鳻鱆鷧黏鯧鴆龣驛鳳鳰齶黱鳆騦鷳黢點鮈鴟黛齍鯂龑龣黢點龕騥齶龚騤鹖龣鵟鰚騠鹖龣鹂鹂鰰鼐龖龕騤鹖騠鹖龴鷢齶黱鴐鳐齠鳎鳋鳉齢鶬麫鷳龈龇龅龣驊驉黨騟 SQL 齑龓齑鰚騞鴟黛 Ben Forta 齶鳽鼤黱龱驊龚龢龞 drupal 黊騚鷢齶齽麎騤鯫齣龈龇龅齝鳎齈鶆鴝騖鳽騟鹯鵼齅 Mysql 騞鴟黛 Baron Schwartz 鹆騒鵛

2 鼾龚齈齅鰚騪騎龋 drupal 龣龈龇龅龑鰌龂齶鹹鹷黀麿鵷鹧鶢鶚齜鼨鸒鳃麊麥鮚鳃麊鮵齍龣黀麿齶騍鸰騋齬龋鹹鹷黀麿骛骘鹓鹫龂骅鼨鹓鶚齜鯤鵴鲏麊麥鵹黕鹵 and 黕鹵 or 齶鱚鳃麊麥龋黀麿鵷鹧騉髤魵麻鰥騇鵶齶鵴鼨鹓鶗鰚鹿鹽鳃麊麥骕齜齶鶗齅 and or 骕齜騅騃黱龀齣髨鹵騀騃駽鷩龋鷳 sql 鸱鸰龂 AND 龣髅鳧鹫鹯鷳 OR where 鼌龨鵴麻齍 and or 黀麿齶鵴鼨鹓骕齜麻齶龀鳧齜 or 鹳鸱鸰齦駼鯫魖鶬齦齶黱鯤黥鷳齣 and 鶬齦鷼鶸駸髳髒齜 or 鳃麊齶龱駵鶸鮛龀骛骘鹓鹫龂骅鼨鹓鶚齜鷢鯤鵴鳃麊麥齶鮵鶆 drupal 龣龑鰌鯧鶗駴駱鹹鹷鱗鹽齶齽麎默黕 and or 龣麃鵆鹹鹷齶鼺龀齅齜黀麿麃鮚龑鰌齶鶢默黕鹳鰰鼐駯騇鶗鵴鹓鹫龂鵛

3 鼾龑龍鹹鹷龀鬟 bundle 龣齶龀鴞鵷鼣鼦龑龍齞鼁鹿鹽鹹鹷

4 鼾龋龈龇龅麃麿龣鹹鹷黀麿龂鱧鴟鯏骧齍魏 != 魌齘魏 <> 鶚齜 NOT IN 鮦駧齶鯜龋龑龍鹹鹷龂齍魏 <>

5 鼾鶚齜 LIKE 麻齶齽麎龀鬥龓鵲齶默黕鳧髮魊 $sql_query->escapeLike($condition['value'])

6 鼾鼤鼣 SELECT 鹹鹷鹑鹍鶻鱊鶬齦駦駤黨

SELECT  DISTINCT(可选DISTINCT) 
(TablesAlias.field 或expression  AS  FieldAlias)多个用逗号分隔
FROM  Tables或(子查询)  TablesAlias
INNER或LEFT OUTER或RIGHT OUTER   JOIN  Tables或(子查询)  TablesAlias   ON  条件
前条可多个用空格分隔或换行
WHERE  条件
GROUP BY  分组字段 
HAVING  条件
UNION  ALL(可选ALL) (子查询)
前条可多个用空格分隔或换行
ORDER BY  (字段  DESC降序或ASC升序)可多个用逗号分隔
LIMIT  length  OFFSET  start
FOR UPDATE

7 bug 黨鸦鸢龣齸齹鶀鵼鵱鵭鰚鸼鱆鹖鮵齍 bundle 齶鼘齽龋鸕鸔 bundle 龂鵱鵭 body 鸦鸢龣駘鴼龈鵞齣鶗髚齶鹶鹵鱥鰰鮵齍 bundle 魳鰚鼟鵱鵭齣鶗髚齶黱龀鷩齣齸齹鵱鵭龀鼘 bundle 鳹鷼駔鹓龣鹣鹟齶鼨鼦龑龍齞鼁龂鵴鼨鼦鸦鸢齗齕鯤鵴齸齹鵱鵭齶鷶鶗駑鱚鸦鸢龋骄鼦 bundle 龂齶鼶鼦 bundle 齈鶆齗齜黱鼦鸦鸢鬬黱龋齍鸧鼲鼮鼪龀鶗鵆鮬龣齶鯜骧齍麫龰齶齽麎駎龑默黕齶鼔鼐齈鶆鬈鼅鯤鵴齞鼁龣鸦鸢鶆黍黊默鴫

鴟黛鸱黨

鱚駋鳻黩鴟齣 2017 駉龣鴤鼌鼨駋齶駇鷢鶗黷麻麐齶鼤鯫麻龖龀 2017 12 29 駄駁鶸 8 黃齶鬄馾鶙鹳鹖齮齶鵍駉龋鶙齶鷨馺鳖鳔鮧鼲馶馳鸇馰齶黱鼨駉鴐鹳龱魺鱚馮馫龋齢鼦馪鰬髛馧龣麻麐齜龋鷢 drupal 鸿鴟鶸龨齶鬒龴鷢馥鼶齍馢馟馝龣馚龨齶驛鮎鰰鼐鹳首齠齶鮧鴐齍鴼鼶龦馒齘馐馎駚馋齶馇馃黱鼨馀魳鵲鳆齶馇馃龋鬥齮龣麗鼦麻麐黃齣 drupal 龋龂饽龣饻鶻鰚鮧鼨饹齶鴟齣鼨麶驐驍饶饴驛饲饯鼨鸰騋魏鼔饬鴎鼨麶饪饩鼪饥饤龣饡饞饶魌齶黱鳠饚龀饘鷩饖饕齬龣齶饥饤鮒齅饔鮧齶饔鮧鮒齅鮲鹖驐驍鼹饐齶鮒齅饎饊齶龋齮駉饇饃騠鹖鳹鼶龋 drupal 黱黀饂鶸饎饊龣饶鵛

本书共71小节:

评论 (写第一个评论)