:::: MENU ::::

Magento Tutorial | Magento Blog | Learn Magento 2

Cookies Consent Popup

In Magento 2, updating product attributes can sometimes be tricky. By default, when you save a product, Magento attempts to update all attributes, which can cause performance issues. For example, saving a single product with all attributes may take 40–50 seconds. If you only need to update one attribute, there’s a faster and more efficient way.

Problem with Full Product Save

Using $product->save() or productRepository->save() updates the entire product object. This is resource‑intensive and unnecessary if you only want to change one attribute.

Example (not recommended):

$item->setWidth(10);
$item->save();

This approach saves the entire product, not just the width attribute.

Solution: Use updateAttributes()

Magento provides the updateAttributes() method in Magento\Catalog\Model\Product\Action to update specific attributes directly. This method is much faster and avoids saving the whole product.

Example Code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productAction = $objectManager->get('Magento\Catalog\Model\Product\Action');

$productAction->updateAttributes(
    [$item->getId()],
    ['width' => 10],
    $storeId
);

Here’s what each parameter means:

  • $productIds → Array of product IDs to update.
  • $attrData → Key‑value pairs of attribute codes and values.
  • $storeId → Store view ID where the attribute should be updated.

Another Example

$this->action->updateAttributes(
    [$productObj->getId()],
    ['your_attribute_code' => 'your_value'],
    $storeId
);

Performance Benefits

  • Updates only the required attribute.
  • Reduces execution time significantly compared to full product save.
  • Prevents unnecessary reindexing of unrelated attributes.

Best Practices

  • Use updateAttributes() for bulk updates (e.g., changing prices or stock status for multiple products).
  • Always specify the correct storeId to avoid overwriting values in unintended store views.
  • Log changes for debugging and auditing purposes.
  • Test updates in a staging environment before applying to production.

Conclusion

Instead of saving the entire product object, use updateAttributes() to update only the required attribute. This method is faster, safer, and more efficient, especially when dealing with large catalogs. By following this approach, you can optimize your Magento 2 store’s performance and reduce unnecessary overhead.

Happy Coding & Optimizing!

Magento 2 introduced the Declarative Schema approach starting from version 2.3. This allows developers to define database structures (tables, columns, indexes, constraints) in XML files instead of writing install/upgrade scripts. One important part of this system is the db_schema_whitelist.json file, which helps Magento track and validate schema changes.

What is db_schema_whitelist.json?

The db_schema_whitelist.json file is automatically generated by Magento when you use the declarative schema. It contains a list of tables and columns that Magento recognizes as part of the schema. This file ensures that:

  • Magento knows which columns are safe to modify.
  • Schema changes are tracked consistently.
  • Developers avoid accidental drops or overwrites of important database structures.

When Do You Need It?

You need to generate db_schema_whitelist.json whenever:

  • You add new tables or columns via db_schema.xml.
  • You modify existing schema definitions.
  • You upgrade Magento and want to ensure schema consistency.

Step 1: Define Schema in db_schema.xml

Inside your module, create or update etc/db_schema.xml with your table definition:

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">

    <table name="vendor_module_example" resource="default" engine="innodb">
        <column xsi:type="int" name="entity_id" nullable="false" identity="true" unsigned="true" comment="Entity ID"/>
        <column xsi:type="varchar" name="title" nullable="false" length="255" comment="Title"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
    </table>

</schema>

Step 2: Generate db_schema_whitelist.json

Run the following command from your Magento root directory:

bin/magento setup:db-declaration:generate-whitelist

This command scans your db_schema.xml and generates db_schema_whitelist.json in your module’s etc directory.

Step 3: Verify the File

The generated db_schema_whitelist.json will look something like this:

{
    "vendor_module_example": {
        "column": {
            "entity_id": true,
            "title": true
        }
    }
}

This confirms that Magento recognizes the columns and will not drop them during future upgrades.

Step 4: Commit to Version Control

Always commit db_schema_whitelist.json to your repository along with db_schema.xml. This ensures consistency across environments (local, staging, production).

Troubleshooting

  • If the file is not generated, check that your db_schema.xml is valid and follows the schema definition rules.
  • Run bin/magento setup:upgrade after generating the whitelist to apply changes.
  • Ensure your module is enabled (bin/magento module:enable Vendor_Module).

Best Practices

  • Always regenerate db_schema_whitelist.json after modifying db_schema.xml.
  • Do not edit db_schema_whitelist.json manually — let Magento generate it.
  • Use declarative schema for new modules instead of legacy install/upgrade scripts.
  • Test schema changes in a staging environment before deploying to production.

Conclusion

The db_schema_whitelist.json file is a critical part of Magento’s declarative schema system. It ensures safe and consistent database changes across environments. By defining your schema in db_schema.xml and generating the whitelist file, you can avoid common issues with missing tables or dropped columns during upgrades.

Happy Coding & Schema Managing!