Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeRepository.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Eav\Model;
8 
14 
19 {
23  protected $eavConfig;
24 
28  protected $eavResource;
29 
34 
39 
43  protected $attributeFactory;
44 
48  protected $joinProcessor;
49 
53  private $collectionProcessor;
54 
65  public function __construct(
66  \Magento\Eav\Model\Config $eavConfig,
69  \Magento\Eav\Api\Data\AttributeSearchResultsInterfaceFactory $searchResultsFactory,
71  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
72  CollectionProcessorInterface $collectionProcessor = null
73  ) {
74  $this->eavConfig = $eavConfig;
75  $this->eavResource = $eavResource;
76  $this->attributeCollectionFactory = $attributeCollectionFactory;
77  $this->searchResultsFactory = $searchResultsFactory;
78  $this->attributeFactory = $attributeFactory;
79  $this->joinProcessor = $joinProcessor;
80  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
81  }
82 
86  public function save(\Magento\Eav\Api\Data\AttributeInterface $attribute)
87  {
88  try {
89  $this->eavResource->save($attribute);
90  } catch (\Exception $e) {
91  throw new StateException(__("The attribute can't be saved."));
92  }
93  return $attribute;
94  }
95 
100  {
101  if (!$entityTypeCode) {
102  throw InputException::requiredField('entity_type_code');
103  }
104 
106  $attributeCollection = $this->attributeCollectionFactory->create();
107  $this->joinProcessor->process($attributeCollection);
108  $attributeCollection->addFieldToFilter('entity_type_code', ['eq' => $entityTypeCode]);
109  $attributeCollection->join(
110  ['entity_type' => $attributeCollection->getTable('eav_entity_type')],
111  'main_table.entity_type_id = entity_type.entity_type_id',
112  []
113  );
114  $attributeCollection->joinLeft(
115  ['eav_entity_attribute' => $attributeCollection->getTable('eav_entity_attribute')],
116  'main_table.attribute_id = eav_entity_attribute.attribute_id',
117  []
118  );
119  $entityType = $this->eavConfig->getEntityType($entityTypeCode);
120 
121  $additionalTable = $entityType->getAdditionalAttributeTable();
122  if ($additionalTable) {
123  $attributeCollection->join(
124  ['additional_table' => $attributeCollection->getTable($additionalTable)],
125  'main_table.attribute_id = additional_table.attribute_id',
126  []
127  );
128  }
129 
130  $this->collectionProcessor->process($searchCriteria, $attributeCollection);
131 
132  // Group attributes by id to prevent duplicates with different attribute sets
133  $attributeCollection->addAttributeGrouping();
134 
135  $attributes = [];
137  foreach ($attributeCollection as $attribute) {
138  $attributes[] = $this->get($entityTypeCode, $attribute->getAttributeCode());
139  }
140 
142  $searchResults = $this->searchResultsFactory->create();
143  $searchResults->setSearchCriteria($searchCriteria);
144  $searchResults->setItems($attributes);
145 
146  // if $searchCriteria has no page size - we can use count() on $attributeCollection
147  // otherwise - we have to use getSize() on $attributeCollection
148  // with this approach we can eliminate excessive COUNT requests in case page size is empty
149  if ($searchCriteria->getPageSize()) {
150  $searchResults->setTotalCount($attributeCollection->getSize());
151  } else {
152  $searchResults->setTotalCount(count($attributeCollection));
153  }
154 
155  return $searchResults;
156  }
157 
161  public function get($entityTypeCode, $attributeCode)
162  {
164  $attribute = $this->eavConfig->getAttribute($entityTypeCode, $attributeCode);
165  if (!$attribute || !$attribute->getAttributeId()) {
166  throw new NoSuchEntityException(
167  __(
168  'The attribute with a "%1" attributeCode doesn\'t exist. Verify the attribute and try again.',
170  )
171  );
172  }
173  return $attribute;
174  }
175 
180  {
181  try {
182  $this->eavResource->delete($attribute);
183  } catch (\Exception $e) {
184  throw new StateException(__("The attribute can't be deleted."));
185  }
186  return true;
187  }
188 
192  public function deleteById($attributeId)
193  {
195  $attribute = $this->attributeFactory->create();
196  $this->eavResource->load($attribute, $attributeId);
197 
198  if (!$attribute->getAttributeId()) {
199  throw new NoSuchEntityException(
200  __('The attribute with a "%1" ID doesn\'t exist. Verify the attribute and try again.', $attributeId)
201  );
202  }
203 
204  $this->delete($attribute);
205  return true;
206  }
207 
214  private function getCollectionProcessor()
215  {
216  if (!$this->collectionProcessor) {
217  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
218  CollectionProcessor::class
219  );
220  }
221  return $this->collectionProcessor;
222  }
223 }
__construct(\Magento\Eav\Model\Config $eavConfig, \Magento\Eav\Model\ResourceModel\Entity\Attribute $eavResource, \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory $attributeCollectionFactory, \Magento\Eav\Api\Data\AttributeSearchResultsInterfaceFactory $searchResultsFactory, \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor, CollectionProcessorInterface $collectionProcessor=null)
__()
Definition: __.php:13
$searchCriteria
$attributeCode
Definition: extend.phtml:12
save(\Magento\Eav\Api\Data\AttributeInterface $attribute)
$attributes
Definition: matrix.phtml:13
getList($entityTypeCode, \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)