Solo para aclarar la respuesta de Jaimin:
Esto funcionará para cualquier entidad.
Esto no es verdad. Solo funcionará para entidades EAV que se extiendanMagento\Eav\Model\Entity\AbstractEntity
Si se trata de una entidad no EAV donde se extiende el modelo de recursos Magento\Framework\Model\ResourceModel\Db\AbstractDb, deberá implementar el saveAttributemétodo en su modelo de recursos.
En Magento 2, lo han hecho para la Magento\Sales\Model\ResourceModel\Attributeclase:
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;
}