Skip to content

Instantly share code, notes, and snippets.

@tshabatyn
Created December 10, 2018 14:06
Show Gist options
  • Select an option

  • Save tshabatyn/506cd1ca10ac81da5b167f569a36adaf to your computer and use it in GitHub Desktop.

Select an option

Save tshabatyn/506cd1ca10ac81da5b167f569a36adaf to your computer and use it in GitHub Desktop.
Magento save Product vs saveAttribute
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product_id = 4;
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
$start = microtime(true);
$productResource = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product');
$product->setName('new product name 1');
$productResource->saveAttribute($product, 'name');
echo "Save attribute duration: ".(microtime(true) - $start)." seconds\n";
$starttime = microtime(true);
$product->setName('new product name 2');
$product->save();
echo "Save product duration: ".(microtime(true) - $start)." seconds\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment