Attribute.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\ResourceModel;
  7. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  8. use Magento\Framework\App\ResourceConnection as AppResource;
  9. use Magento\Framework\Event\ManagerInterface as EventManager;
  10. use Magento\Sales\Model\AbstractModel;
  11. class Attribute
  12. {
  13. /**
  14. * @var \Magento\Framework\App\ResourceConnection
  15. */
  16. protected $resource;
  17. /**
  18. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  19. */
  20. protected $connection;
  21. /**
  22. * @var \Magento\Framework\Event\ManagerInterface
  23. */
  24. protected $eventManager;
  25. /**
  26. * @param AppResource $resource
  27. * @param EventManager $eventManager
  28. */
  29. public function __construct(
  30. AppResource $resource,
  31. EventManager $eventManager
  32. ) {
  33. $this->resource = $resource;
  34. $this->eventManager = $eventManager;
  35. }
  36. /**
  37. * @return \Magento\Framework\DB\Adapter\AdapterInterface
  38. */
  39. protected function getConnection()
  40. {
  41. if (!$this->connection) {
  42. $this->connection = $this->resource->getConnection('sales');
  43. }
  44. return $this->connection;
  45. }
  46. /**
  47. * Before save object attribute
  48. *
  49. * @param AbstractModel $object
  50. * @param string $attribute
  51. * @return \Magento\Sales\Model\ResourceModel\Attribute
  52. */
  53. protected function _beforeSaveAttribute(AbstractModel $object, $attribute)
  54. {
  55. if ($object->getEventObject() && $object->getEventPrefix()) {
  56. $this->eventManager->dispatch(
  57. $object->getEventPrefix() . '_save_attribute_before',
  58. [
  59. $object->getEventObject() => $this,
  60. 'object' => $object,
  61. 'attribute' => $attribute
  62. ]
  63. );
  64. }
  65. return $this;
  66. }
  67. /**
  68. * Perform actions after object save
  69. *
  70. * @param AbstractModel $object
  71. * @param string $attribute
  72. * @return $this
  73. * @throws \Exception
  74. */
  75. public function saveAttribute(AbstractModel $object, $attribute)
  76. {
  77. if ($attribute instanceof AbstractAttribute) {
  78. $attributes = $attribute->getAttributeCode();
  79. } elseif (is_string($attribute)) {
  80. $attributes = [$attribute];
  81. } else {
  82. $attributes = $attribute;
  83. }
  84. if (is_array($attributes) && !empty($attributes)) {
  85. $this->getConnection()->beginTransaction();
  86. $data = array_intersect_key($object->getData(), array_flip($attributes));
  87. try {
  88. $this->_beforeSaveAttribute($object, $attributes);
  89. if ($object->getId() && !empty($data)) {
  90. $this->getConnection()->update(
  91. $object->getResource()->getMainTable(),
  92. $data,
  93. [$object->getResource()->getIdFieldName() . '= ?' => (int)$object->getId()]
  94. );
  95. $object->addData($data);
  96. }
  97. $this->_afterSaveAttribute($object, $attributes);
  98. $this->getConnection()->commit();
  99. } catch (\Exception $e) {
  100. $this->getConnection()->rollBack();
  101. throw $e;
  102. }
  103. }
  104. return $this;
  105. }
  106. /**
  107. * After save object attribute
  108. *
  109. * @param AbstractModel $object
  110. * @param string $attribute
  111. * @return \Magento\Sales\Model\ResourceModel\Attribute
  112. */
  113. protected function _afterSaveAttribute(AbstractModel $object, $attribute)
  114. {
  115. if ($object->getEventObject() && $object->getEventPrefix()) {
  116. $this->eventManager->dispatch(
  117. $object->getEventPrefix() . '_save_attribute_after',
  118. [
  119. $object->getEventObject() => $this,
  120. 'object' => $object,
  121. 'attribute' => $attribute
  122. ]
  123. );
  124. }
  125. return $this;
  126. }
  127. }