Just to clarify Jaimin's answer:
This will work for any entity.
This is not true. It will only work for EAV entities which extend Magento\Eav\Model\Entity\AbstractEntity
If you're dealing with a non EAV entity where the resource model extends Magento\Framework\Model\ResourceModel\Db\AbstractDb
you will have to implement the saveAttribute
method in your resource model.
In Magento 2, they have done it for the Magento\Sales\Model\ResourceModel\Attribute
class:
public function saveAttribute(AbstractModel $object, $attribute)
{
if ($attribute instanceof AbstractAttribute) {
$attributes = $attribute->getAttributeCode();
} elseif (is_string($attribute)) {
$attributes = [$attribute];
} else {
$attributes = $attribute;
}
if (is_array($attributes) && !empty($attributes)) {
$this->getConnection()->beginTransaction();
$data = array_intersect_key($object->getData(), array_flip($attributes));
try {
$this->_beforeSaveAttribute($object, $attributes);
if ($object->getId() && !empty($data)) {
$this->getConnection()->update(
$object->getResource()->getMainTable(),
$data,
[$object->getResource()->getIdFieldName() . '= ?' => (int)$object->getId()]
);
$object->addData($data);
}
$this->_afterSaveAttribute($object, $attributes);
$this->getConnection()->commit();
} catch (\Exception $e) {
$this->getConnection()->rollBack();
throw $e;
}
}
return $this;
}