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 黵黱齣鷚鷗麠鷺鷶龖龓龒齮齰鷲鹝龖龓龒齮黤龖鸲

本书共65小节:

评论 (写第一个评论)