resource = $resource; $this->eventManager = $eventManager; } /** * @return \Magento\Framework\DB\Adapter\AdapterInterface */ protected function getConnection() { if (!$this->connection) { $this->connection = $this->resource->getConnection('sales'); } return $this->connection; } /** * Before save object attribute * * @param AbstractModel $object * @param string $attribute * @return \Magento\Sales\Model\ResourceModel\Attribute */ protected function _beforeSaveAttribute(AbstractModel $object, $attribute) { if ($object->getEventObject() && $object->getEventPrefix()) { $this->eventManager->dispatch( $object->getEventPrefix() . '_save_attribute_before', [ $object->getEventObject() => $this, 'object' => $object, 'attribute' => $attribute ] ); } return $this; } /** * Perform actions after object save * * @param AbstractModel $object * @param string $attribute * @return $this * @throws \Exception */ 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; } /** * After save object attribute * * @param AbstractModel $object * @param string $attribute * @return \Magento\Sales\Model\ResourceModel\Attribute */ protected function _afterSaveAttribute(AbstractModel $object, $attribute) { if ($object->getEventObject() && $object->getEventPrefix()) { $this->eventManager->dispatch( $object->getEventPrefix() . '_save_attribute_after', [ $object->getEventObject() => $this, 'object' => $object, 'attribute' => $attribute ] ); } return $this; } }