Skip to content

Instantly share code, notes, and snippets.

@aczietlow
Last active July 28, 2017 16:20
Show Gist options
  • Save aczietlow/bd1e20fd483974f3efc7fdabae7101c5 to your computer and use it in GitHub Desktop.
Save aczietlow/bd1e20fd483974f3efc7fdabae7101c5 to your computer and use it in GitHub Desktop.
Faster way of updating single field of an entity
<?php
function updateProductCategoryReference($product_id, $new_cat_tid) {
$product = new stdClass();
$product->product_id = $product_id;
$product->type = 'product';
$product->field_product_category[LANGUAGE_NONE][]['tid'] = $new_cat_tid;
$transaction = db_transaction();
try {
field_attach_presave('commerce_product', $product);
field_attach_update('commerce_product', $product);
entity_get_controller('commerce_product')->resetCache([$product_id]);
}
catch (\Exception $e) {
$transaction->rollback();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment