13
13
use Magento \Eav \Model \Entity \Attribute \Frontend \AbstractFrontend ;
14
14
use Magento \Eav \Model \Entity \Attribute \Source \AbstractSource ;
15
15
use Magento \Framework \App \Config \Element ;
16
+ use Magento \Framework \DataObject ;
16
17
use Magento \Framework \DB \Adapter \DuplicateException ;
17
18
use Magento \Framework \Exception \AlreadyExistsException ;
18
19
use Magento \Framework \Exception \LocalizedException ;
@@ -62,6 +63,13 @@ abstract class AbstractEntity extends AbstractResource implements EntityInterfac
62
63
*/
63
64
protected $ _attributesByCode = [];
64
65
66
+ /**
67
+ * Attributes stored by scope (store id and attribute set id).
68
+ *
69
+ * @var array
70
+ */
71
+ private $ attributesByScope = [];
72
+
65
73
/**
66
74
* Two-dimensional array by table name and attribute name
67
75
*
@@ -473,6 +481,46 @@ public function addAttribute(AbstractAttribute $attribute)
473
481
return $ this ;
474
482
}
475
483
484
+ /**
485
+ * Adding attribute to entity by scope.
486
+ *
487
+ * @param AbstractAttribute $attribute
488
+ * @param DataObject|null $entity
489
+ * @return $this
490
+ */
491
+ public function addAttributeByScope (AbstractAttribute $ attribute , $ entity = null )
492
+ {
493
+ $ suffix = $ entity !== null ? $ this ->getAttributesCacheSuffix ($ entity ) : '0-0 ' ;
494
+ $ attributeCode = $ attribute ->getAttributeCode ();
495
+ $ this ->attributesByScope [$ suffix ][$ attributeCode ] = $ attribute ;
496
+ return $ this ->addAttribute ($ attribute );
497
+ }
498
+
499
+ /**
500
+ * Get attributes by scope
501
+ *
502
+ * @return array
503
+ */
504
+ private function getAttributesByScope ($ suffix )
505
+ {
506
+ return !empty ($ this ->attributesByScope [$ suffix ])
507
+ ? $ this ->attributesByScope [$ suffix ]
508
+ : $ this ->getAttributesByCode ();
509
+ }
510
+
511
+ /**
512
+ * Get attributes cache suffix.
513
+ *
514
+ * @param DataObject $object
515
+ * @return string
516
+ */
517
+ private function getAttributesCacheSuffix (DataObject $ object )
518
+ {
519
+ $ attributeSetId = $ object ->getAttributeSetId () ?: 0 ;
520
+ $ storeId = $ object ->getStoreId () ?: 0 ;
521
+ return $ storeId . '- ' . $ attributeSetId ;
522
+ }
523
+
476
524
/**
477
525
* Retrieve partial load flag
478
526
*
@@ -506,7 +554,7 @@ public function isPartialSave($flag = null)
506
554
/**
507
555
* Retrieve configuration for all attributes
508
556
*
509
- * @param null|\Magento\Framework\ DataObject $object
557
+ * @param null|DataObject $object
510
558
* @return $this
511
559
*/
512
560
public function loadAllAttributes ($ object = null )
@@ -566,7 +614,7 @@ public function attributesCompare($firstAttribute, $secondAttribute)
566
614
/**
567
615
* Check whether the attribute is Applicable to the object
568
616
*
569
- * @param \Magento\Framework\ DataObject $object
617
+ * @param DataObject $object
570
618
* @param AbstractAttribute $attribute
571
619
* @return bool
572
620
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
@@ -611,7 +659,8 @@ public function walkAttributes($partMethod, array $args = [], $collectExceptionM
611
659
break ;
612
660
}
613
661
$ results = [];
614
- foreach ($ this ->getAttributesByCode () as $ attrCode => $ attribute ) {
662
+ $ suffix = $ this ->getAttributesCacheSuffix ($ args [0 ]);
663
+ foreach ($ this ->getAttributesByScope ($ suffix ) as $ attrCode => $ attribute ) {
615
664
if (isset ($ args [0 ]) && is_object ($ args [0 ]) && !$ this ->_isApplicableAttribute ($ args [0 ], $ attribute )) {
616
665
continue ;
617
666
}
@@ -830,7 +879,7 @@ public function isAttributeStatic($attribute)
830
879
/**
831
880
* Validate all object's attributes against configuration
832
881
*
833
- * @param \Magento\Framework\ DataObject $object
882
+ * @param DataObject $object
834
883
* @throws \Magento\Eav\Model\Entity\Attribute\Exception
835
884
* @return true|array
836
885
*/
@@ -856,10 +905,10 @@ public function validate($object)
856
905
/**
857
906
* Set new increment id to object
858
907
*
859
- * @param \Magento\Framework\ DataObject $object
908
+ * @param DataObject $object
860
909
* @return $this
861
910
*/
862
- public function setNewIncrementId (\ Magento \ Framework \ DataObject $ object )
911
+ public function setNewIncrementId (DataObject $ object )
863
912
{
864
913
if ($ object ->getIncrementId ()) {
865
914
return $ this ;
@@ -878,7 +927,7 @@ public function setNewIncrementId(\Magento\Framework\DataObject $object)
878
927
* Check attribute unique value
879
928
*
880
929
* @param AbstractAttribute $attribute
881
- * @param \Magento\Framework\ DataObject $object
930
+ * @param DataObject $object
882
931
* @return bool
883
932
*/
884
933
public function checkAttributeUniqueValue (AbstractAttribute $ attribute , $ object )
@@ -1051,7 +1100,7 @@ protected function _prepareLoadSelect(array $selects)
1051
1100
/**
1052
1101
* Retrieve select object for loading base entity row
1053
1102
*
1054
- * @param \Magento\Framework\ DataObject $object
1103
+ * @param DataObject $object
1055
1104
* @param string|int $rowId
1056
1105
* @return \Magento\Framework\DB\Select
1057
1106
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
@@ -1071,7 +1120,7 @@ protected function _getLoadRowSelect($object, $rowId)
1071
1120
/**
1072
1121
* Retrieve select object for loading entity attributes values
1073
1122
*
1074
- * @param \Magento\Framework\ DataObject $object
1123
+ * @param DataObject $object
1075
1124
* @param string $table
1076
1125
* @return \Magento\Framework\DB\Select
1077
1126
*/
@@ -1091,7 +1140,7 @@ protected function _getLoadAttributesSelect($object, $table)
1091
1140
/**
1092
1141
* Initialize attribute value for object
1093
1142
*
1094
- * @param \Magento\Framework\ DataObject $object
1143
+ * @param DataObject $object
1095
1144
* @param array $valueRow
1096
1145
* @return $this
1097
1146
*/
@@ -1181,8 +1230,8 @@ protected function processSave($object)
1181
1230
/**
1182
1231
* Retrieve Object instance with original data
1183
1232
*
1184
- * @param \Magento\Framework\ DataObject $object
1185
- * @return \Magento\Framework\ DataObject
1233
+ * @param DataObject $object
1234
+ * @return DataObject
1186
1235
*/
1187
1236
protected function _getOrigObject ($ object )
1188
1237
{
@@ -1422,7 +1471,7 @@ protected function _processSaveData($saveData)
1422
1471
/**
1423
1472
* Process base row
1424
1473
*/
1425
- $ entityObject = new \ Magento \ Framework \ DataObject ($ entityRow );
1474
+ $ entityObject = new DataObject ($ entityRow );
1426
1475
$ entityRow = $ this ->_prepareDataForTable ($ entityObject , $ entityTable );
1427
1476
if ($ insertEntity ) {
1428
1477
if (!empty ($ entityId )) {
@@ -1477,7 +1526,7 @@ protected function _processSaveData($saveData)
1477
1526
/**
1478
1527
* Insert entity attribute value
1479
1528
*
1480
- * @param \Magento\Framework\ DataObject $object
1529
+ * @param DataObject $object
1481
1530
* @param AbstractAttribute $attribute
1482
1531
* @param mixed $value
1483
1532
* @return $this
@@ -1490,7 +1539,7 @@ protected function _insertAttribute($object, $attribute, $value)
1490
1539
/**
1491
1540
* Update entity attribute value
1492
1541
*
1493
- * @param \Magento\Framework\ DataObject $object
1542
+ * @param DataObject $object
1494
1543
* @param AbstractAttribute $attribute
1495
1544
* @param mixed $valueId
1496
1545
* @param mixed $value
@@ -1585,10 +1634,10 @@ protected function _prepareValueForSave($value, AbstractAttribute $attribute)
1585
1634
/**
1586
1635
* Delete entity attribute values
1587
1636
*
1588
- * @param \Magento\Framework\ DataObject $object
1637
+ * @param DataObject $object
1589
1638
* @param string $table
1590
1639
* @param array $info
1591
- * @return \Magento\Framework\ DataObject
1640
+ * @return DataObject
1592
1641
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
1593
1642
*/
1594
1643
protected function _deleteAttributes ($ object , $ table , $ info )
@@ -1614,13 +1663,13 @@ protected function _deleteAttributes($object, $table, $info)
1614
1663
/**
1615
1664
* Save attribute
1616
1665
*
1617
- * @param \Magento\Framework\ DataObject $object
1666
+ * @param DataObject $object
1618
1667
* @param string $attributeCode
1619
1668
* @return $this
1620
1669
* @throws \Exception
1621
1670
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1622
1671
*/
1623
- public function saveAttribute (\ Magento \ Framework \ DataObject $ object , $ attributeCode )
1672
+ public function saveAttribute (DataObject $ object , $ attributeCode )
1624
1673
{
1625
1674
$ attribute = $ this ->getAttribute ($ attributeCode );
1626
1675
$ backend = $ attribute ->getBackend ();
@@ -1666,8 +1715,8 @@ public function saveAttribute(\Magento\Framework\DataObject $object, $attributeC
1666
1715
/**
1667
1716
* Return attribute row to prepare where statement
1668
1717
*
1669
- * @param \Magento\Framework\ DataObject $entity
1670
- * @param \Magento\Framework\ DataObject $object
1718
+ * @param DataObject $entity
1719
+ * @param DataObject $object
1671
1720
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
1672
1721
* @return array
1673
1722
*/
@@ -1688,7 +1737,7 @@ protected function getAttributeRow($entity, $object, $attribute)
1688
1737
/**
1689
1738
* Delete entity using current object's data
1690
1739
*
1691
- * @param \Magento\Framework\ DataObject|int|string $object
1740
+ * @param DataObject|int|string $object
1692
1741
* @return $this
1693
1742
* @throws \Exception
1694
1743
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
@@ -1730,7 +1779,7 @@ public function delete($object)
1730
1779
/**
1731
1780
* Evaluate Delete operations
1732
1781
*
1733
- * @param \Magento\Framework\ DataObject|int|string $object
1782
+ * @param DataObject|int|string $object
1734
1783
* @param string|int $id
1735
1784
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
1736
1785
* @return void
@@ -1764,10 +1813,10 @@ protected function evaluateDelete($object, $id, $connection)
1764
1813
/**
1765
1814
* After Load Entity process
1766
1815
*
1767
- * @param \Magento\Framework\ DataObject $object
1816
+ * @param DataObject $object
1768
1817
* @return $this
1769
1818
*/
1770
- protected function _afterLoad (\ Magento \ Framework \ DataObject $ object )
1819
+ protected function _afterLoad (DataObject $ object )
1771
1820
{
1772
1821
\Magento \Framework \Profiler::start ('after_load ' );
1773
1822
$ this ->walkAttributes ('backend/afterLoad ' , [$ object ]);
@@ -1778,10 +1827,10 @@ protected function _afterLoad(\Magento\Framework\DataObject $object)
1778
1827
/**
1779
1828
* Before delete Entity process
1780
1829
*
1781
- * @param \Magento\Framework\ DataObject $object
1830
+ * @param DataObject $object
1782
1831
* @return $this
1783
1832
*/
1784
- protected function _beforeSave (\ Magento \ Framework \ DataObject $ object )
1833
+ protected function _beforeSave (DataObject $ object )
1785
1834
{
1786
1835
$ this ->walkAttributes ('backend/beforeSave ' , [$ object ]);
1787
1836
return $ this ;
@@ -1790,10 +1839,10 @@ protected function _beforeSave(\Magento\Framework\DataObject $object)
1790
1839
/**
1791
1840
* After Save Entity process
1792
1841
*
1793
- * @param \Magento\Framework\ DataObject $object
1842
+ * @param DataObject $object
1794
1843
* @return $this
1795
1844
*/
1796
- protected function _afterSave (\ Magento \ Framework \ DataObject $ object )
1845
+ protected function _afterSave (DataObject $ object )
1797
1846
{
1798
1847
$ this ->walkAttributes ('backend/afterSave ' , [$ object ]);
1799
1848
return $ this ;
@@ -1802,10 +1851,10 @@ protected function _afterSave(\Magento\Framework\DataObject $object)
1802
1851
/**
1803
1852
* Before Delete Entity process
1804
1853
*
1805
- * @param \Magento\Framework\ DataObject $object
1854
+ * @param DataObject $object
1806
1855
* @return $this
1807
1856
*/
1808
- protected function _beforeDelete (\ Magento \ Framework \ DataObject $ object )
1857
+ protected function _beforeDelete (DataObject $ object )
1809
1858
{
1810
1859
$ this ->walkAttributes ('backend/beforeDelete ' , [$ object ]);
1811
1860
return $ this ;
@@ -1814,10 +1863,10 @@ protected function _beforeDelete(\Magento\Framework\DataObject $object)
1814
1863
/**
1815
1864
* After delete entity process
1816
1865
*
1817
- * @param \Magento\Framework\ DataObject $object
1866
+ * @param DataObject $object
1818
1867
* @return $this
1819
1868
*/
1820
- protected function _afterDelete (\ Magento \ Framework \ DataObject $ object )
1869
+ protected function _afterDelete (DataObject $ object )
1821
1870
{
1822
1871
$ this ->walkAttributes ('backend/afterDelete ' , [$ object ]);
1823
1872
return $ this ;
@@ -1887,54 +1936,54 @@ protected function getAttributeLoader()
1887
1936
/**
1888
1937
* Perform actions after entity load
1889
1938
*
1890
- * @param \Magento\Framework\ DataObject $object
1939
+ * @param DataObject $object
1891
1940
* @since 100.1.0
1892
1941
*/
1893
- public function afterLoad (\ Magento \ Framework \ DataObject $ object )
1942
+ public function afterLoad (DataObject $ object )
1894
1943
{
1895
1944
$ this ->_afterLoad ($ object );
1896
1945
}
1897
1946
1898
1947
/**
1899
1948
* Perform actions before entity save
1900
1949
*
1901
- * @param \Magento\Framework\ DataObject $object
1950
+ * @param DataObject $object
1902
1951
* @since 100.1.0
1903
1952
*/
1904
- public function beforeSave (\ Magento \ Framework \ DataObject $ object )
1953
+ public function beforeSave (DataObject $ object )
1905
1954
{
1906
1955
$ this ->_beforeSave ($ object );
1907
1956
}
1908
1957
1909
1958
/**
1910
1959
* Perform actions after entity save
1911
1960
*
1912
- * @param \Magento\Framework\ DataObject $object
1961
+ * @param DataObject $object
1913
1962
* @since 100.1.0
1914
1963
*/
1915
- public function afterSave (\ Magento \ Framework \ DataObject $ object )
1964
+ public function afterSave (DataObject $ object )
1916
1965
{
1917
1966
$ this ->_afterSave ($ object );
1918
1967
}
1919
1968
1920
1969
/**
1921
1970
* Perform actions before entity delete
1922
1971
*
1923
- * @param \Magento\Framework\ DataObject $object
1972
+ * @param DataObject $object
1924
1973
* @since 100.1.0
1925
1974
*/
1926
- public function beforeDelete (\ Magento \ Framework \ DataObject $ object )
1975
+ public function beforeDelete (DataObject $ object )
1927
1976
{
1928
1977
$ this ->_beforeDelete ($ object );
1929
1978
}
1930
1979
1931
1980
/**
1932
1981
* Perform actions after entity delete
1933
1982
*
1934
- * @param \Magento\Framework\ DataObject $object
1983
+ * @param DataObject $object
1935
1984
* @since 100.1.0
1936
1985
*/
1937
- public function afterDelete (\ Magento \ Framework \ DataObject $ object )
1986
+ public function afterDelete (DataObject $ object )
1938
1987
{
1939
1988
$ this ->_afterDelete ($ object );
1940
1989
}
0 commit comments