Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractModel.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Model;
7 
9 
18 {
27  protected $_defaultValues;
28 
34  protected $_storeValuesFlags;
35 
41  protected $_lockedAttributes = [];
42 
48  protected $_isDeleteable = true;
49 
55  protected $_isReadonly = false;
56 
62  protected $_storeManager;
63 
67  private $scopeOverriddenValue;
68 
79  public function __construct(
80  \Magento\Framework\Model\Context $context,
81  \Magento\Framework\Registry $registry,
82  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
85  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
86  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
87  array $data = []
88  ) {
89  $this->_storeManager = $storeManager;
90  parent::__construct(
91  $context,
92  $registry,
93  $extensionFactory,
95  $resource,
96  $resourceCollection,
97  $data
98  );
99  }
100 
107  public function lockAttribute($attributeCode)
108  {
109  $this->_lockedAttributes[$attributeCode] = true;
110  return $this;
111  }
112 
120  {
121  if ($this->isLockedAttribute($attributeCode)) {
122  unset($this->_lockedAttributes[$attributeCode]);
123  }
124 
125  return $this;
126  }
127 
133  public function unlockAttributes()
134  {
135  $this->_lockedAttributes = [];
136  return $this;
137  }
138 
144  public function getLockedAttributes()
145  {
146  return array_keys($this->_lockedAttributes);
147  }
148 
154  public function hasLockedAttributes()
155  {
156  return !empty($this->_lockedAttributes);
157  }
158 
166  {
167  return isset($this->_lockedAttributes[$attributeCode]);
168  }
169 
184  public function setData($key, $value = null)
185  {
186  if ($this->hasLockedAttributes()) {
187  if (is_array($key)) {
188  foreach ($this->getLockedAttributes() as $attribute) {
189  if (isset($key[$attribute])) {
190  unset($key[$attribute]);
191  }
192  }
193  } elseif ($this->isLockedAttribute($key)) {
194  return $this;
195  }
196  } elseif ($this->isReadonly()) {
197  return $this;
198  }
199 
200  return parent::setData($key, $value);
201  }
202 
213  public function unsetData($key = null)
214  {
215  if ($key !== null && $this->isLockedAttribute($key) || $this->isReadonly()) {
216  return $this;
217  }
218 
219  return parent::unsetData($key);
220  }
221 
228  public function getResourceCollection()
229  {
230  $collection = parent::getResourceCollection()->setStoreId($this->getStoreId());
231  return $collection;
232  }
233 
242  public function loadByAttribute($attribute, $value, $additionalAttributes = '*')
243  {
244  $collection = $this->getResourceCollection()->addAttributeToSelect(
245  $additionalAttributes
246  )->addAttributeToFilter(
247  $attribute,
248  $value
249  )->setPage(
250  1,
251  1
252  );
253 
254  foreach ($collection as $object) {
255  return $object;
256  }
257  return false;
258  }
259 
265  public function getStore()
266  {
267  return $this->_storeManager->getStore($this->getStoreId());
268  }
269 
275  public function getWebsiteStoreIds()
276  {
277  return $this->getStore()->getWebsite()->getStoreIds(true);
278  }
279 
292  {
293  $this->_defaultValues[$attributeCode] = $value;
294  return $this;
295  }
296 
304  private function getAttributeScopeOverriddenValue()
305  {
306  if ($this->scopeOverriddenValue === null) {
307  $this->scopeOverriddenValue = \Magento\Framework\App\ObjectManager::getInstance()
308  ->get(\Magento\Catalog\Model\Attribute\ScopeOverriddenValue::class);
309  }
310  return $this->scopeOverriddenValue;
311  }
312 
322  {
323  if ($this->_defaultValues === null) {
324  $entityType = [
325  \Magento\Catalog\Model\Product::ENTITY => \Magento\Catalog\Api\Data\ProductInterface::class,
326  \Magento\Catalog\Model\Category::ENTITY => \Magento\Catalog\Api\Data\CategoryInterface::class,
327  ][$this->getResource()->getEntityType()->getEntityTypeCode()];
328  $this->_defaultValues = $this->getAttributeScopeOverriddenValue()->getDefaultValues($entityType, $this);
329  }
330 
331  return array_key_exists($attributeCode, $this->_defaultValues) ? $this->_defaultValues[$attributeCode] : false;
332  }
333 
344  {
345  $this->_storeValuesFlags[$attributeCode] = true;
346  return $this;
347  }
348 
359  {
360  if ($this->_storeValuesFlags === null) {
361  $entityType = [
362  \Magento\Catalog\Model\Product::ENTITY => \Magento\Catalog\Api\Data\ProductInterface::class,
363  \Magento\Catalog\Model\Category::ENTITY => \Magento\Catalog\Api\Data\CategoryInterface::class,
364  ][$this->getResource()->getEntityType()->getEntityTypeCode()];
365  return $this->getAttributeScopeOverriddenValue()->containsValue(
366  $entityType,
367  $this,
369  $this->getStore()->getId()
370  );
371  }
372 
373  return array_key_exists($attributeCode, $this->_storeValuesFlags);
374  }
375 
381  public function beforeSave()
382  {
383  $this->unlockAttributes();
384  return parent::beforeSave();
385  }
386 
392  public function isDeleteable()
393  {
394  return $this->_isDeleteable;
395  }
396 
403  public function setIsDeleteable($value)
404  {
405  $this->_isDeleteable = (bool)$value;
406  return $this;
407  }
408 
414  public function isReadonly()
415  {
416  return $this->_isReadonly;
417  }
418 
425  public function setIsReadonly($value)
426  {
427  $this->_isReadonly = (bool)$value;
428  return $this;
429  }
430 }
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$storeManager
$resource
Definition: bulk.php:12
setAttributeDefaultValue($attributeCode, $value)
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
loadByAttribute($attribute, $value, $additionalAttributes=' *')