mirror of
https://github.com/bdnugget/magento2.git
synced 2026-11-07 17:27:36 +00:00
149 lines
4.3 KiB
PHP
149 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* Copyright © Magento, Inc. All rights reserved.
|
|
* See COPYING.txt for license details.
|
|
*/
|
|
|
|
namespace Magento\Sales\Model\ResourceModel;
|
|
|
|
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
|
|
use Magento\Framework\App\ResourceConnection as AppResource;
|
|
use Magento\Framework\Event\ManagerInterface as EventManager;
|
|
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
|
|
use Magento\Sales\Model\AbstractModel;
|
|
|
|
class Attribute implements ResetAfterRequestInterface
|
|
{
|
|
/**
|
|
* @var \Magento\Framework\App\ResourceConnection
|
|
*/
|
|
protected $resource;
|
|
|
|
/**
|
|
* @var \Magento\Framework\DB\Adapter\AdapterInterface|null
|
|
*/
|
|
protected $connection;
|
|
|
|
/**
|
|
* @var \Magento\Framework\Event\ManagerInterface
|
|
*/
|
|
protected $eventManager;
|
|
|
|
/**
|
|
* @param AppResource $resource
|
|
* @param EventManager $eventManager
|
|
*/
|
|
public function __construct(
|
|
AppResource $resource,
|
|
EventManager $eventManager
|
|
) {
|
|
$this->resource = $resource;
|
|
$this->eventManager = $eventManager;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function _resetState(): void
|
|
{
|
|
$this->connection = null;
|
|
}
|
|
|
|
/**
|
|
* Gets the connection, lazy loading it if needed
|
|
*
|
|
* @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 mixed $attribute
|
|
* @return $this
|
|
* @throws \Exception
|
|
*/
|
|
public function saveAttribute(AbstractModel $object, mixed $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;
|
|
}
|
|
}
|