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 黾黺齬鷨鷥麪鸈鸄龚龘龗齹齼鸁鹮龚龘龗齹黭龚鹂

本书共69小节:

评论 (写第一个评论)