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 黠黝齛鷙鷘麎鷱鷰龑龍龊齪齮鷭鹉龑龍龊齪黋龑鸨

本书共88小节:

评论 (写第一个评论)