Config.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel;
  7. /**
  8. * Eav Resource Config model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Config extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  13. {
  14. /**
  15. * Array of entity types
  16. *
  17. * @var array
  18. */
  19. protected static $_entityTypes = [];
  20. /**
  21. * Array of attributes
  22. *
  23. * @var array
  24. */
  25. protected static $_attributes = [];
  26. /**
  27. * Resource initialization
  28. *
  29. * @return void
  30. * @codeCoverageIgnore
  31. */
  32. protected function _construct()
  33. {
  34. $this->_init('eav_entity_type', 'entity_type_id');
  35. }
  36. /**
  37. * Load all entity types
  38. *
  39. * @return $this
  40. */
  41. protected function _loadTypes()
  42. {
  43. $connection = $this->getConnection();
  44. if (!$connection) {
  45. return $this;
  46. }
  47. if (empty(self::$_entityTypes)) {
  48. $select = $connection->select()->from($this->getMainTable());
  49. $data = $connection->fetchAll($select);
  50. foreach ($data as $row) {
  51. self::$_entityTypes['by_id'][$row['entity_type_id']] = $row;
  52. self::$_entityTypes['by_code'][$row['entity_type_code']] = $row;
  53. }
  54. }
  55. return $this;
  56. }
  57. /**
  58. * Load attribute types
  59. *
  60. * @param int $typeId
  61. * @return array
  62. */
  63. protected function _loadTypeAttributes($typeId)
  64. {
  65. if (!isset(self::$_attributes[$typeId])) {
  66. $connection = $this->getConnection();
  67. $bind = ['entity_type_id' => $typeId];
  68. $select = $connection->select()->from(
  69. $this->getTable('eav_attribute')
  70. )->where(
  71. 'entity_type_id = :entity_type_id'
  72. );
  73. self::$_attributes[$typeId] = $connection->fetchAll($select, $bind);
  74. }
  75. return self::$_attributes[$typeId];
  76. }
  77. /**
  78. * Retrieve entity type data
  79. *
  80. * @param string $entityType
  81. * @return array
  82. */
  83. public function fetchEntityTypeData($entityType)
  84. {
  85. $this->_loadTypes();
  86. if (is_numeric($entityType)) {
  87. $info = isset(
  88. self::$_entityTypes['by_id'][$entityType]
  89. ) ? self::$_entityTypes['by_id'][$entityType] : null;
  90. } else {
  91. $info = isset(
  92. self::$_entityTypes['by_code'][$entityType]
  93. ) ? self::$_entityTypes['by_code'][$entityType] : null;
  94. }
  95. $data = [];
  96. if ($info) {
  97. $data['entity'] = $info;
  98. $attributes = $this->_loadTypeAttributes($info['entity_type_id']);
  99. $data['attributes'] = [];
  100. foreach ($attributes as $attribute) {
  101. $data['attributes'][$attribute['attribute_id']] = $attribute;
  102. $data['attributes'][$attribute['attribute_code']] = $attribute['attribute_id'];
  103. }
  104. }
  105. return $data;
  106. }
  107. }