In last blog we discussed about some optimisation tips.
You can refer the blog. It might help you.
Optimisation Tips For Magento And Magento2 – Part 1
Today i will tell you about some more good methods to optimise the code and application.
Updating Product Attribute
if we want to update any attribute value of Product, we load product and then we update the attribute value in product like name, price etc.
There is a method updateAttributes which is more effective and faster in terms of optimisation.
//Magento 1 $productId = 1; $storeId = 1; Mage::getResourceModel('catalog/product')->updateAttributes([$productId], ['attribute_code' => "attribute value"], $storeId); //Magento 2 $objManager = \Magento\Framework\App\ObjectManager::getInstance(); $productId = 1; $storeId = 1; $objManager->create('Magento\Catalog\Model\ResourceModel\Product\Action')->updateAttributes([$productId], ['attribute_code' => "attribute value"], $storeId);
You can use this method to update multiple products at same time.
//Magento 2 $objManager = \Magento\Framework\App\ObjectManager::getInstance(); $productIds = [1,2,3]; $storeId = 0; $objManager->create('Magento\Catalog\Model\ResourceModel\Product\Action')->updateAttributes($productIds, ['status' => 1], $storeId);
Inserting Multiple Records
Many times it happens, we need to insert multiple records into the table.
For example if we want to insert 100 records in table, we will simply load the model and insert the data into the table. But this is not the best way when we want to insert multiple records into table.
We should use database transactions. One of the benefits of using transaction is that we can rollback the transaction if something went wrong.
You can refer the blog to know more about database transactions.
Working With Database Transactions In Magento2
//Magento 1 try { $data = []; $data[] = ["field1" => "value1", "field2" => "value2"]; $data[] = ["field1" => "value3", "field2" => "value4"]; $resource = Mage::getSingleton('core/resource'); $connection = $resource->getConnection('core_write'); $connection->beginTransaction(); $connection->insertMultiple($resource->getTableName('custom_table'), $data); $connection->commit(); } catch(Exception $e) { $connection->rollBack(); } //Magento 2 try { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $connection = $objectManager->create('Magento\Framework\Module\ModuleResource')->getConnection(); $resource = $objectManager->create('Magento\Framework\App\ResourceConnection'); $data = []; $data[] = ["field1" => "value1", "field2" => "value2"]; $data[] = ["field1" => "value3", "field2" => "value4"]; $connection->beginTransaction(); $connection->insertMultiple($resource->getTableName('custom_table'), $data); $connection->commit(); } catch(\Exception $e) { $connection->rollBack(); }
insertMultiple method is really very effective when you are inserting large number of records.
By using this method you can insert 1000 of records without any deadlock or execution time errors.
If you have any doubt or query please comment below.
Thanks
Be the first to comment.