23
23
* @copyright Since 2007 PrestaShop SA and Contributors
24
24
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25
25
*/
26
+ use PrestaShop \PrestaShop \Adapter \ObjectModelComparator ;
26
27
use PrestaShop \PrestaShop \Adapter \ServiceLocator ;
27
28
use PrestaShop \PrestaShop \Core \Image \ImageFormatConfiguration ;
28
29
use PrestaShopBundle \Translation \TranslatorComponent ;
@@ -153,6 +154,13 @@ abstract class ObjectModelCore implements PrestaShop\PrestaShop\Core\Foundation\
153
154
*/
154
155
protected static $ cache_objects = true ;
155
156
157
+ /**
158
+ * Skip the update process when no change detected to improve performances
159
+ *
160
+ * @var bool
161
+ */
162
+ public $ skipUpdateIfUnchanged = false ;
163
+
156
164
/**
157
165
* @return null
158
166
*/
@@ -686,12 +694,36 @@ public function duplicateObject()
686
694
*
687
695
* @throws PrestaShopDatabaseException
688
696
* @throws PrestaShopException
697
+ * @throws PrestaShop\PrestaShop\Core\Exception\InvalidArgumentException
689
698
*/
690
699
public function update ($ null_values = false )
700
+ {
701
+ $ currentClassName = $ this ->getObjectName ();
702
+ $ oldObject = new $ currentClassName ($ this ->id , $ this ->id_lang , $ this ->id_shop );
703
+ $ comparator = new ObjectModelComparator ($ oldObject , $ this );
704
+
705
+ if ($ this ->skipUpdateIfUnchanged && !$ comparator ->hasChanges ()) {
706
+ return true ;
707
+ }
708
+
709
+ return $ this ->processUpdateAction ((bool ) $ null_values , $ comparator );
710
+ }
711
+
712
+ /**
713
+ * Updates the current object in the database.
714
+ *
715
+ * @param bool $null_values
716
+ * @param ObjectModelComparator|null $comparator
717
+ * @return bool
718
+ *
719
+ * @throws PrestaShopDatabaseException
720
+ * @throws PrestaShopException
721
+ */
722
+ protected function processUpdateAction (bool $ null_values = false , ObjectModelComparator $ comparator = null ): bool
691
723
{
692
724
// @hook actionObject<ObjectClassName>UpdateBefore
693
- Hook::exec ('actionObjectUpdateBefore ' , ['object ' => $ this ]);
694
- Hook::exec ('actionObject ' . $ this ->getFullyQualifiedName () . 'UpdateBefore ' , ['object ' => $ this ]);
725
+ Hook::exec ('actionObjectUpdateBefore ' , ['object ' => $ this , ' objectComparator ' => $ comparator ]);
726
+ Hook::exec ('actionObject ' . $ this ->getFullyQualifiedName () . 'UpdateBefore ' , ['object ' => $ this , ' objectComparator ' => $ comparator ]);
695
727
696
728
$ this ->clearCache ();
697
729
@@ -779,8 +811,8 @@ public function update($null_values = false)
779
811
foreach ($ id_shop_list as $ id_shop ) {
780
812
$ field ['id_shop ' ] = (int ) $ id_shop ;
781
813
$ where = pSQL ($ this ->def ['primary ' ]) . ' = ' . (int ) $ this ->id
782
- . ' AND id_lang = ' . (int ) $ field ['id_lang ' ]
783
- . ' AND id_shop = ' . (int ) $ id_shop ;
814
+ . ' AND id_lang = ' . (int ) $ field ['id_lang ' ]
815
+ . ' AND id_shop = ' . (int ) $ id_shop ;
784
816
785
817
if (Db::getInstance ()->getValue ('SELECT COUNT(*) FROM ' . pSQL (_DB_PREFIX_ . $ this ->def ['table ' ]) . '_lang WHERE ' . $ where )) {
786
818
$ result &= Db::getInstance ()->update ($ this ->def ['table ' ] . '_lang ' , $ field , $ where );
@@ -791,7 +823,7 @@ public function update($null_values = false)
791
823
} else {
792
824
// If this table is not linked to multishop system ...
793
825
$ where = pSQL ($ this ->def ['primary ' ]) . ' = ' . (int ) $ this ->id
794
- . ' AND id_lang = ' . (int ) $ field ['id_lang ' ];
826
+ . ' AND id_lang = ' . (int ) $ field ['id_lang ' ];
795
827
if (Db::getInstance ()->getValue ('SELECT COUNT(*) FROM ' . pSQL (_DB_PREFIX_ . $ this ->def ['table ' ]) . '_lang WHERE ' . $ where )) {
796
828
$ result &= Db::getInstance ()->update ($ this ->def ['table ' ] . '_lang ' , $ field , $ where );
797
829
} else {
@@ -803,8 +835,8 @@ public function update($null_values = false)
803
835
}
804
836
805
837
// @hook actionObject<ObjectClassName>UpdateAfter
806
- Hook::exec ('actionObjectUpdateAfter ' , ['object ' => $ this ]);
807
- Hook::exec ('actionObject ' . $ this ->getFullyQualifiedName () . 'UpdateAfter ' , ['object ' => $ this ]);
838
+ Hook::exec ('actionObjectUpdateAfter ' , ['object ' => $ this , ' objectComparator ' => $ comparator ]);
839
+ Hook::exec ('actionObject ' . $ this ->getFullyQualifiedName () . 'UpdateAfter ' , ['object ' => $ this , ' objectComparator ' => $ comparator ]);
808
840
809
841
return $ result ;
810
842
}
0 commit comments