Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed PR #523, prevent unnecessary saving of status history when orde… #2042

Conversation

kiatng
Copy link
Contributor

@kiatng kiatng commented Mar 25, 2022

…r is saved.

Description (*)

Order is saved when comments are added in the backend. Before this PR, this will save the new comment (status history object) along with all the old status histories which are unnecessary. After this PR, only the new comment is saved.

Related Pull Requests

PR #523

Fixed Issues (if relevant)

Manual testing scenarios (*)

To test this, add test code in app\code\core\Mage\Sales\Model\Order\Status\History.php

    public function save()
    {
        Mage::helper('clog')->log("sh#{$this->getId()}"); // use your own log
        return parent::save();
    }
   //...
    protected function _beforeSave()
    {
        Mage::helper('clog')->log("bsh#{$this->getId()}"); // use your own log
       //...

Test result before PR:

[25-03-2022 15:59:52][kiat Administrators] sh#5774133
[25-03-2022 15:59:52][kiat Administrators] bsh#5774133
[25-03-2022 15:59:52][kiat Administrators] sh#5774132
[25-03-2022 15:59:52][kiat Administrators] bsh#5774132
[25-03-2022 15:59:52][kiat Administrators] sh#5774131
[25-03-2022 15:59:52][kiat Administrators] bsh#5774131
[25-03-2022 15:59:52][kiat Administrators] sh#5773982
[25-03-2022 15:59:52][kiat Administrators] bsh#5773982
[25-03-2022 15:59:52][kiat Administrators] sh#
[25-03-2022 15:59:52][kiat Administrators] bsh#

Test result after PR:

[25-03-2022 16:00:49][kiat Administrators] sh#5774134
[25-03-2022 16:00:49][kiat Administrators] sh#5774133
[25-03-2022 16:00:49][kiat Administrators] sh#5774132
[25-03-2022 16:00:49][kiat Administrators] sh#5774131
[25-03-2022 16:00:49][kiat Administrators] sh#5773982
[25-03-2022 16:00:49][kiat Administrators] sh#
[25-03-2022 16:00:49][kiat Administrators] bsh#

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (all builds are green)
  • Add yourself to contributors list

@github-actions github-actions bot added the Component: Sales Relates to Mage_Sales label Mar 25, 2022
@kiatng kiatng added the performance Performance related label Mar 29, 2022
foreach ($this->_statusHistory as $status) {
$status->setOrder($this);
$status->setDataChanges(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems we need to add this setDataChanges calls in a lot of places, but doesn't it mean we should modify some Abstract or something like that to solve this issue in a more general way?

@kiatng
Copy link
Contributor Author

kiatng commented Aug 20, 2022

@fballiano is right, there should be a general way to solve this, but I have not found the way to do it.

@elidrissidev
Copy link
Member

While the current change will work fine, I found that all collections suffer from this issue! This could be fixed in all places by applying the below patch to lib/Varien/Data/Collection/Db.php, and keeping the change from the original PR to prevent setStoreId() call from changing _hasDataChanges to true again (tested).

diff --git a/lib/Varien/Data/Collection/Db.php b/lib/Varien/Data/Collection/Db.php
index 147f25ca3d..ff85dcaada 100644
--- a/lib/Varien/Data/Collection/Db.php
+++ b/lib/Varien/Data/Collection/Db.php
@@ -587,6 +587,7 @@ class Varien_Data_Collection_Db extends Varien_Data_Collection
                     $item->setIdFieldName($this->getIdFieldName());
                 }
                 $item->addData($row);
+                $item->setDataChanges(false);
                 $this->addItem($item);
             }
         }

What do you think?

@kiatng
Copy link
Contributor Author

kiatng commented Jan 27, 2023

@elidrissidev It's a good idea. Can you create a PR and close this one?

@elidrissidev
Copy link
Member

@kiatng Sure. Will do it as soon as I get some time.

@kiatng kiatng deleted the prevent_unnecessary_status_history_save branch February 27, 2023 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Sales Relates to Mage_Sales performance Performance related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants