43. 实体entity(一):实体基类

龺龸 龴龲龯龮龪龧龤龡龞龛龘龖龕龑龎龌龉龇龃龁齿齼齹龸齷龑龎齶齵齲齯齷龑龎齭齩齧齤齡齟龑龎齭齩齟龑龎齲齝齛龮齙齘齝齖龴齼齹齔齐齌齊齉齆齂龎齿鼿鼾鼺鼹齝鼷鼳鼯龯鼫鼨鼧龖龑龎齿齼齹齝鼥鼯龯齌鼣鼟鼛鼚齿鼙鼯鼖

鼔鼑龑龎齶鼐鼧鼏鼋鼇鼅龸

龑龎鼁黾龴黺龸黹黸黶黲黯齯鼏鼋黫黧黤黠

龑龎齭齩鼁黾龸黹黸龑龎齿黟黲黯齝黜黛龕龑龎齭黚黙黗齿黔黒黐黍

龑龎黋齤齡龸鼏龲龯黇鼁龑龎黋齤齿鼏鼋黅黄齝黃黁黸齟麾麽麺麹齟龯麶麴麳齟麶麱麴麳齟麯麮麪麨齟麤麡黲黯齟麤麡麴麳麟麟齝麞鼋黋齤齡麚黔黒黟黲黯黗鼣黚齝麘鼏麗齭齩齿龑龎鼁麓齿黋齤齡麑麏麍麉鼏麆齿齝麂麂麍麁黚齿齭

龑龎齭齩鼁黾鹿鹾鹺麱齝齼鼇麍鹸龞鹵鹲鹱麹齌黚黙麚龑龎鹵鹲黔黒黗齿黟黲黯龺齉齆鼥鼁黾黗齝鹯龕黅黄齝鼁鹮鼇齿黟黲黯黃鹫鹪鼧黚黙齿鹩鹦鹤鹡鹠鹝鹙鹗齝麚鹖黧龞鹔麑鼳鹸龞 set 黤黠鼁黟鹎鹊鹇鹄鹁鹀齝鹝鹙龑鸼麍龸

Drupal\Core\Entity\ EntityType 龸鸜 Drupal\Core\Entity\ EntityTypeInterface

鸼麚龲鸐黗鼧龛鸍齭龑龎龸鸋鸊龑龎齯鸈鹠龑龎齝鸄黯鸂鷾鷻鷺鷶鷴鷱鷰鷯龑鸼鷬龑龎齭齩鼁黾齝鷰鷯鷪鷨鷤黛鼳鷠齿龑龎齭齩龌齭龸

Drupal\Core\Entity\ContentEntityType Drupal\Core\Entity\ContentEntityTypeInterface

Drupal\Core\Config\Entity\ConfigEntityType Drupal\Core\Config\Entity\ConfigEntityTypeInterface

鼇齤鷛龑龎龮齖龑龎龌齭齔齐龸

Drupal\Core\Entity\Entity (Drupal\Core\Entity\EntityInterface)

龑龎鷓鷏鷍鷌麪鷉鷬龑龎齿龌龴黤黠齝鼥齭麍龲鸐黚黙齿鼏鷅龑龎鷃黾龌齭齝鸋鸊龑龎龌齭龸

Drupal\Core\Entity\ContentEntityBase (Drupal\Core\Entity\ContentEntityInterface)

齯鸈鹠龑龎龌齭

Drupal\Core\Config\Entity\ConfigEntityBase (Drupal\Core\Config\Entity\ConfigEntityInterface)

鷀鷨鷤鶿齝鶾龛鶽鶼鶹鶷麗齂龎齿龑龎鷨鷤齝鶳麞麆鼏鶰鼏鶰齿龑鸼鷬龑龎龲鸐齝鶭鶪鼛鼚龑龎龮齖鼥龑龎龌齭齔齐齝麞鶩麍龴龘齿齼齹鼖

Entity 齭麍龲鸐黗鶥鼧龑龎齿龌齭齝鶣鶠黚黙鷬龑龎齿鹸黧龌龴黤黠齝麚鶥鼧龑龎鼁黾鷠鷀麑鶟黧齝麞麍鼏鷅鼁龑龎齶鼐鶜鷉鷃黾鶾齿鶜鶘黍鷶齝鶕鼇鶑鼺齤鷛麘鼏鷅黤黠齝麚龴龲龯齿龑龎齶齵齼齹黗鶏鷬鶋鶇龲鸐齿龯鶅黜鶁鵽龑龎齝麑鼳鵹鵷龑龎鼁黾龴鵶鷠鶳麍鼏鷅鵲鵮鵬齭齩鹎鹊齿龑鵫黲黯鼷龑鸼鷬黄黧麚麞鼋黲黯鷠齿黤黠齿鼁黾齝鶭鶪 Entity 龌齭齿麴鵧鵤黲鼧龛鷅鵣黲龸

public function __construct(array $values, $entity_type)

鵔鼏鷅鵣黲 $values 麶鵒鼇鵑齉齿鹎鹊黲黯齝麍鼏鷅龖鵍黲鵊齝龑龎鼁黾黧鶿齿鵈鵅黄鷱龑龎鵁龇鵅齝鵔鴽鷅鵣黲 $entity_type 麍鼏鷅鴼鴺鴷鴳鴲鴺齝麶鵒鵑齉齿麍鴯鼏齭齩齿鹎鹊齝鶩鶳麍龑龎齭齩 id 齝龑龎鼁黾鴪黯鼥鹎鹊齖龑龎齭齩齧齤齡黗鴦鴣鵲鵮龑龎黟黲黯齿龑龎齭齩鼁黾齝齖鴢鴟鴞鴜鴛鴗黅黄鵑齉齿黲黯鼖

鼳鹤麍龑龎龌齭黗齿鼇鼅鶁鵽齝鹺麱黤黠麉鴓鶁鵽鼖

鴒鴑龑龎麍鴏鷱鶋龸

鹝鹙齿鹪鼧龑龎 id 鴌鹡鹠鷬鴈麹鷱鶋鴇鹙鷱龑龎麍鶋麳齿齝鴆鴄黹黸齝鼥鹝鹙龑鸼鴃鼁龕鸋鸊龑龎麍鼧鴂齿齝鳾麉鳻鳹龕鸈鹠龑龎齝鶭鶪鸈鹠龑龎龌齭鼁鷰鹇鹄鷬鳵鳱齝鴈麹鷱鶋麍鷱鷬鸈鹠齿鼟齉鼟鵷麟麁鳯鶕鳮

龑龎鳬鷓龸

龑龎鶕鼇龯麶鳬鷓齟齙鵹鳬鷓齟鶷麗麶麱鳬鷓麟麟齝鳬鷓 url 鼧鼏黚鳩鳨齝鼁麓黶黛鳧齿麯麮鼁黾齝麚黟黲黯齿 links 鳣黗黚黙鷬鳬鷓鳡鳝齝鳡鳝黗麑鼳鼧鼳鹤鳜鳙鴺齝黧鸍鳘鼨鵲鳗齝麚鳔鳓鳬鷓鳑鳎鶹鳊鳉鷱齂龎龑龎齿齂龎鹗龸

龑龎齭齩鵅龸龑龎 id

齭齩黚黙黗 bundle_entity_type 齿鹗 鴌鶽麚齭齩黚黙鶣 entity_keys 黗齿 Bundle 齿鹗龸龑龎 bundle() 齿鹗

龑龎齭齩鵅 +'_revision' 龸鲾龴 id 齝鶩鶳麍龑龎黤黠 getRevisionId() 齿鹗

鳬鷓黅黄黗鳎鲸鲷 Url Link 鼁黾齝鼁鶿鷯齿龃龁麚鲮鲬齿齼齹黗鹇鹄

麾麽麺麹龸

麞鷶鷱龛鸍齭龸麍鴏鼧鲪鲧麳齟鲧麳鶾麑鼳鹇鹄鲣鴛黅黄

鲢黸鴳鲞龸

鲢黸鴳鲞麑鼳鹸鴞齆鲢黸龲鸐龑龎麍鴏龧鲝鲙鲗齝鼳鹯鲷鳑鲓鲢黸鲏鴂齝麘鷅龑龎鼧黛黺齿鹸黧鴳鲞齝鴳鲞鵅鷱龸龑龎齭齩 id+ 鲌龸鲊 + 龑龎 id

麑鼳鲉龑龎鹡鹠 cacheTags 鵁龇齝鼳鼣黚鲂鲬齿鲢黸鴳鲞齝麞齯龑龎黛黺齿鹸黧鴳鲞鼏鱿麴鱽龑龎鴳鲞齝鶩鶳麍龑龎龸 getCacheTags() 黤黠齿鹗

龑龎齿龯麶鴳鲞麚龑龎黔黒黗齿 list_cache_tags 鳣鼣黚齝黃鹫鹪鼧鹡鹠齝齌麍鹝鹙齿龸龑龎齭齩 id+ '_list' 齝麞麑鼳鹸龞龑龎齭齩齭齿 getListCacheTags() 黤黠鴟齆

黹黸龑龎鳑鲏鴂鼳鹤鲢黸鴳鲞龸

龑龎龯麶鸜鼿麶黶黧鱺鵹齆齿龯麶鱶鸑齟 4xx-response 鸜龑龎鹀鲙麑麏鼟鱱 4xx 鱭鱩鼾鲏鴂鸑齟黛黺鹸黧鴳鲞鸜鹸鴞黧齆鼥龑龎齿鱥黤鲏鴂鲢黸鸑

黫黧鱤鱩黤黠龸

load($id) loadMultiple(array $ids = NULL) create(array $values = []) 麍鱤鱩黤黠齝麞鱓鱐黶麑鼳麚齭龑鱌鲗鱉龪鶟黧齝麞鼋黅黄鶕鼇鴞鱇龑龎齭齩 id 齝鳾鶪鳑鱆麉鴞鱇齝鱄鴛黃鱁鴟鴞鰿麑鼳鹸龞齭鵅齝麞黧齆鷬鸊齡黗齿鼳鹤鰾鰺龸

鸊齡 id 龸鲌 entity_type.repository

齭龸 Drupal\Core\Entity\EntityTypeRepository

鼥鰾鰺麪麨龛鷅鰷鰵黤黠龸

getEntityTypeFromClass($class_name) 龸鹸龞鰬鰩黚齭鵅黜鴟齆龑龎齭齩 id

getEntityTypeLabels($group = FALSE) 龸鴟齆鶥鼧龑龎齭齩齿鰢齵鴳鲞齝鵣黲鼣黚麍鴏鰟鰞龑龎黔黒黗齿 group_label 鷶鵊鰜鰛齝黃鹫龑龎黔黒黗鹪鼧鹡鹠 group_label 齝鱄鴛鳎鹖黧黔黒鼁黾黗齿鹗齝龲鸐黗鶥鼧龑龎鹝鹙鵁龕龛鷅鷶鵊齝鷶鷴鷱鸋鸊龑龎鵊齯鸈鹠龑龎鵊齝黛黚黙齿龑龎麑鼳鼣黚鼥鴳鲞鳔鳓黛鳧齿鵊齝鹖黧鼥黤黠鵒鱌黃鹤龸

        $Labels= \Drupal::service("entity_type.repository")->getEntityTypeLabels(true);
        foreach ($Labels as $k=>$v)
        {
            echo "分组: ".$k."\n";
            foreach ($v as $a=>$b) {
                echo "   ".$a." : ".$b."\n";
            }
        }

鸈鹠鴪鰍鵈齯鸈鹠鴪鰍鵅龸

鸈鹠龑龎麑鼳鼧鴪鰍鶕鳮齝麑鼳鴪鰍鰊鷰齿鸋鸊龑龎鴌鸈鹠龑龎齝麚鰉鰆鸈鹠龑龎鳑鰃鼧鶥鴪鰍齿龑龎鰬黍黸麚鶿鰂鳎鶹鰉鰆齝鶭鶪麚鸈鹠龑龎龌齭黗黚黙鷬鼏鷅 $dependencies 鵁龇 齝鶿麍鼏鷅黲鵊齝鶣鶠黹黸黶鼥鸈鹠鶕鼇齿鴪鰍齝齂龎鶳麍鴪鰍鵅齝鶿黜黛鶹鴪鰍龑龎齿 getConfigDependencyName() 黤黠齝

麚龲鸐黗鼧鳑鰀鶕鼇齌鴪鰍黁黸麚黲鵊鶣鹇鹄鵑鯿齝鶕鼇鼏鷅黲鵊鵈鵅齝鹝鹙鹤齝鸋鸊龑龎齿鴪鰍鵈鷱鲌 content 鲊齝鸈鹠龑龎齿鴪鰍鵈鷱鲌 config 鲊齝麞鼷麉黧麚龪黒麪齆齿 $dependencies 鵁龇黗齝鼳鶾鳎鼧龎鳎

齭齩鲗黲黯龸

龑龎麑鼳鹸龞齭齩鲗鵊鹲麪麨齿鯾麏鯺鲗鷱鯷鳹齭齩齿齭齩鲗黲黯鼁黾齝麞麆麑鼳鹇鹄鯴鯰黤鹯齿黅黄齝鹿黃黲黯鯬鯩麟麟齝麚鷬鷛龴鼹龪齛龮齘鯥龴龲龯齿齭齩鲗 API 齼齹齝麚龌齭黗齌鼏鷅龑龎鯺鲗鷱齭齩鲗黲黯齿黤黠鷱龸

getTypedData() 齝鯠鯞鼿鼾麍龸

$class = \Drupal::typedDataManager()->getDefinition('entity')['class'];

$this->typedData = $class::createFromEntity($this);



鼥黤黠麚鸈鹠龑龎龌齭齯鸋鸊龑龎龌齭黗鹪鼧鶹鯚鳱齝鸍黍鷶龑龎鯖鷓鹖黧鶿齝鹸龞齭齩鲗黲黯齧齤齡鴟齆齿龑龎鹵鹲鳵鼇黚黙黃鹤龸

class齭齩齭龸Drupal\Core\Entity\Plugin\DataType\EntityAdapter

definition_class黚黙齭龸Drupal\Core\Entity\TypedData\EntityDataDefinition

list_class龯麶齭龸Drupal\Core\TypedData\Plugin\DataType\ItemList

list_definition_class龯麶黚黙齭龸Drupal\Core\TypedData\ListDataDefinition

deriver鯒鳓齭龸Drupal\Core\Entity\Plugin\DataType\Deriver\EntityDeriver

麑鼳鵹鵷麍龸

Drupal\Core\Entity\Plugin\DataType\EntityAdapter::createFromEntity($this);

鲧麳鷬龑龎齿齭齩鲗黲黯鼁黾

鯏龪龑龎齿齭齩鲗鯍麉鯋鯊鸈鹠龑龎齝鯈龸 https://www.drupal.org/node/1818574

鶭鷱齭齩鲗鳎鲸鲷齆鴼鮶 api 齟龑龎鮳鮱齟鹵鹲鯒鳓麟鸋鸊齝齌麚龃龁鮯鲸鲷齿鸋鸊鶾鮮鮫齔鼏鷅齼齹鮧鼺鮥鷛

鮡鮠鶁鵽龸

  1. 龺鱓龑龎龌齭黗鹪鼧麪麨 get set 黤黠齝鷰鷯麚鸋鸊龑龎龌齭齯鸈鹠龑龎龌齭黗龑鸼

本书共63小节:

评论 (写第一个评论)