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 鼂黑馕鷢馠馞龨駏鶔

本书共63小节:

评论 (写第一个评论)