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 黿黐饋鷑饕饑龥馄鵻

本书共88小节:

评论 (写第一个评论)