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

Reduce needless saves by avoiding setting _hasDataChanges flag #2066

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Do not use 20.x.x if you need IE support.
- fixed incorrect datetime in customer block (`$useTimezone` parameter) #1525
- add redis as a valid option for `global/session_save` #1513
- possibility to disable global search in backend #1532
- reduce needless saves by avoiding setting `_hasDataChanges` flag #2066

For full list of changes, you can [compare tags](https://github.com/OpenMage/magento-lts/compare/1.9.4.x...20.0).

Expand Down
34 changes: 31 additions & 3 deletions app/code/core/Mage/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,6 @@
* @method $this setStoreId(int $store)
* @method bool hasStoreIds()
* @method $this setStoreIds(array $storeIds)
* @method Mage_CatalogInventory_Model_Stock_Item getStockItem()
* @method bool hasStockItem()
* @method $this setStockItem(Mage_CatalogInventory_Model_Stock_Item $value)
* @method array getSwatchPrices()
*
* @method int getTaxClassId()
Expand Down Expand Up @@ -344,6 +341,11 @@ class Mage_Catalog_Model_Product extends Mage_Catalog_Model_Abstract
*/
protected $_calculatePrice = true;

/**
* @var Mage_CatalogInventory_Model_Stock_Item
*/
protected $_stockItem;

/**
* Initialize resources
*/
Expand Down Expand Up @@ -694,6 +696,32 @@ public function getAttributes($groupId = null, $skipSuper = false)
return $attributes;
}

/**
* @return Mage_CatalogInventory_Model_Stock_Item
*/
public function getStockItem()
{
return $this->_stockItem;
}

/**
* @return bool
*/
public function hasStockItem()
{
return !!$this->_stockItem;
Copy link
Contributor

Choose a reason for hiding this comment

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

why there is a double negation?

Copy link
Contributor

Choose a reason for hiding this comment

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

it's a cast to boolean as far as I've read

Copy link
Contributor

Choose a reason for hiding this comment

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

I never used it but it is the equivalent (bool) $variables. Maybe the code with this variant !! should be modified to be more explicit for most and not to be able to search the Internet for the "double not" operator in PHP. A longer discussion exists here https://stackoverflow.com/questions/2127260/double-not-operator-in-php. Some people consider using it "fancy".

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, this is a habit from writing C.. In PHP probably better to just use (bool) but they are functionally equivalent.

}

/**
* @param Mage_CatalogInventory_Model_Stock_Item $stockItem
* @return $this
*/
public function setStockItem(Mage_CatalogInventory_Model_Stock_Item $stockItem)
{
$this->_stockItem = $stockItem;
return $this;
}

/**
* Check product options and type options and save them, too
*
Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Customer/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public function getCustomer()
public function setCustomer(Mage_Customer_Model_Customer $customer)
{
$this->_customer = $customer;
$this->setCustomerId($customer->getId());
if ($this->getCustomerId() != $customer->getId()) {
$this->setCustomerId($customer->getId());
}
return $this;
}

Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Sales/Model/Order/Creditmemo/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ public function getCreditmemo()
public function setOrderItem(Mage_Sales_Model_Order_Item $item)
{
$this->_orderItem = $item;
$this->setOrderItemId($item->getId());
if ($this->getOrderItemId() != $item->getId()) {
$this->setOrderItemId($item->getId());
}
return $this;
}

Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Sales/Model/Order/Invoice/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ public function getInvoice()
public function setOrderItem(Mage_Sales_Model_Order_Item $item)
{
$this->_orderItem = $item;
$this->setOrderItemId($item->getId());
if ($this->getOrderItemId() != $item->getId()) {
$this->setOrderItemId($item->getId());
}
return $this;
}

Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Sales/Model/Order/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,9 @@ public function getQtyToCancelBundleItem()
public function setOrder(Mage_Sales_Model_Order $order)
{
$this->_order = $order;
$this->setOrderId($order->getId());
if ($this->getOrderId() != $order->getId()) {
$this->setOrderId($order->getId());
}
return $this;
}

Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Sales/Model/Order/Shipment/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public function getShipment()
public function setOrderItem(Mage_Sales_Model_Order_Item $item)
{
$this->_orderItem = $item;
$this->setOrderItemId($item->getId());
if ($this->getOrderItemId() != $item->getId()) {
$this->setOrderItemId($item->getId());
}
return $this;
}

Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Sales/Model/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ public function getStore()
*/
public function setStore(Mage_Core_Model_Store $store)
{
$this->setStoreId($store->getId());
if ($this->getStoreId() != $store->getId()) {
$this->setStoreId($store->getId());
}
return $this;
}

Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Sales/Model/Quote/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@ protected function _afterSave()
public function setQuote(Mage_Sales_Model_Quote $quote)
{
$this->_quote = $quote;
$this->setQuoteId($quote->getId());
if ($this->getQuoteId() != $quote->getId()) {
$this->setQuoteId($quote->getId());
}
return $this;
}

Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Sales/Model/Quote/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ protected function _beforeSave()
public function setQuote(Mage_Sales_Model_Quote $quote)
{
$this->_quote = $quote;
$this->setQuoteId($quote->getId());
if ($this->getQuoteId() != $quote->getId()) {
$this->setQuoteId($quote->getId());
}
return $this;
}

Expand Down
8 changes: 6 additions & 2 deletions app/code/core/Mage/Sales/Model/Quote/Item/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ protected function _hasModelChanged()
*/
public function setItem($item)
{
$this->setItemId($item->getId());
$this->_item = $item;
if ($this->getItemId() != $item->getId()) {
$this->setItemId($item->getId());
}
return $this;
}

Expand All @@ -106,8 +108,10 @@ public function getItem()
*/
public function setProduct($product)
{
$this->setProductId($product->getId());
$this->_product = $product;
if ($this->getProductId() != $product->getId()) {
$this->setProductId($product->getId());
}
return $this;
}

Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Sales/Model/Quote/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ protected function _construct()
public function setQuote(Mage_Sales_Model_Quote $quote)
{
$this->_quote = $quote;
$this->setQuoteId($quote->getId());
if ($this->getQuoteId() != $quote->getId()) {
$this->setQuoteId($quote->getId());
}
return $this;
}

Expand Down
9 changes: 7 additions & 2 deletions app/code/core/Mage/Wishlist/Model/Item/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @method int getProductId()
* @method $this setProductId(int $value)
* @method $this setWishlistItemId(int $value)
* @method int getWishlistItemId()
* @method $this setValue(string $sBuyRequest)
*/
class Mage_Wishlist_Model_Item_Option extends Mage_Core_Model_Abstract implements Mage_Catalog_Model_Product_Configuration_Item_Option_Interface
Expand Down Expand Up @@ -73,8 +74,10 @@ protected function _hasModelChanged()
*/
public function setItem($item)
{
$this->setWishlistItemId($item->getId());
$this->_item = $item;
if ($this->getWishlistItemId() != $item->getId()) {
$this->setWishlistItemId($item->getId());
}
return $this;
}

Expand All @@ -96,8 +99,10 @@ public function getItem()
*/
public function setProduct($product)
{
$this->setProductId($product->getId());
$this->_product = $product;
if ($this->getProductId() != $product->getId()) {
$this->setProductId($product->getId());
}
return $this;
}

Expand Down