EntityAbstract.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb;
  8. use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite;
  9. use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
  10. use Magento\SalesSequence\Model\Manager;
  11. use Magento\Sales\Model\EntityInterface;
  12. /**
  13. * Abstract sales entity provides to its children knowledge about eventPrefix and eventObject
  14. *
  15. * @api
  16. * @SuppressWarnings(PHPMD.NumberOfChildren)
  17. * @since 100.0.2
  18. */
  19. abstract class EntityAbstract extends AbstractDb
  20. {
  21. /**
  22. * Event prefix
  23. *
  24. * @var string
  25. */
  26. protected $_eventPrefix = 'sales_order_resource';
  27. /**
  28. * Event object
  29. *
  30. * @var string
  31. */
  32. protected $_eventObject = 'resource';
  33. /**
  34. * Use additional is object new check for this resource
  35. *
  36. * @var bool
  37. */
  38. protected $_useIsObjectNew = true;
  39. /**
  40. * @var \Magento\Eav\Model\Entity\TypeFactory
  41. */
  42. protected $_eavEntityTypeFactory;
  43. /**
  44. * @var \Magento\Sales\Model\ResourceModel\Attribute
  45. */
  46. protected $attribute;
  47. /**
  48. * @var Manager
  49. */
  50. protected $sequenceManager;
  51. /**
  52. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  53. * @param Snapshot $entitySnapshot
  54. * @param RelationComposite $entityRelationComposite
  55. * @param Attribute $attribute
  56. * @param Manager $sequenceManager
  57. * @param string $connectionName
  58. */
  59. public function __construct(
  60. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  61. Snapshot $entitySnapshot,
  62. RelationComposite $entityRelationComposite,
  63. \Magento\Sales\Model\ResourceModel\Attribute $attribute,
  64. Manager $sequenceManager,
  65. $connectionName = null
  66. ) {
  67. $this->attribute = $attribute;
  68. $this->sequenceManager = $sequenceManager;
  69. if ($connectionName === null) {
  70. $connectionName = 'sales';
  71. }
  72. parent::__construct($context, $entitySnapshot, $entityRelationComposite, $connectionName);
  73. }
  74. /**
  75. * Perform actions after object save
  76. *
  77. * @param \Magento\Framework\Model\AbstractModel $object
  78. * @param string $attribute
  79. * @return $this
  80. * @throws \Exception
  81. */
  82. public function saveAttribute(\Magento\Framework\Model\AbstractModel $object, $attribute)
  83. {
  84. $this->attribute->saveAttribute($object, $attribute);
  85. return $this;
  86. }
  87. /**
  88. * Prepares data for saving and removes update time (if exists).
  89. * This prevents saving same update time on each entity update.
  90. *
  91. * @param \Magento\Framework\Model\AbstractModel $object
  92. * @return array
  93. */
  94. protected function _prepareDataForSave(\Magento\Framework\Model\AbstractModel $object)
  95. {
  96. $data = parent::_prepareDataForTable($object, $this->getMainTable());
  97. if (isset($data['updated_at'])) {
  98. unset($data['updated_at']);
  99. }
  100. return $data;
  101. }
  102. /**
  103. * Perform actions before object save
  104. * Perform actions before object save, calculate next sequence value for increment Id
  105. *
  106. * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\DataObject $object
  107. * @return $this
  108. */
  109. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  110. {
  111. /** @var \Magento\Sales\Model\AbstractModel $object */
  112. if ($object instanceof EntityInterface && $object->getIncrementId() == null) {
  113. $store = $object->getStore();
  114. $storeId = $store->getId();
  115. if ($storeId === null) {
  116. $storeId = $store->getGroup()->getDefaultStoreId();
  117. }
  118. $object->setIncrementId(
  119. $this->sequenceManager->getSequence(
  120. $object->getEntityType(),
  121. $storeId
  122. )->getNextValue()
  123. );
  124. }
  125. parent::_beforeSave($object);
  126. return $this;
  127. }
  128. /**
  129. * Perform actions after object save
  130. *
  131. * @param \Magento\Framework\Model\AbstractModel $object
  132. * @return $this
  133. */
  134. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  135. {
  136. $connection = $this->getConnection();
  137. $columns = $connection->describeTable($this->getMainTable());
  138. if (isset($columns['created_at'], $columns['updated_at'])) {
  139. $select = $connection->select()
  140. ->from($this->getMainTable(), ['created_at', 'updated_at'])
  141. ->where($this->getIdFieldName() . ' = :entity_id');
  142. $row = $connection->fetchRow($select, [':entity_id' => $object->getId()]);
  143. if (is_array($row) && isset($row['created_at'], $row['updated_at'])) {
  144. $object->setCreatedAt($row['created_at']);
  145. $object->setUpdatedAt($row['updated_at']);
  146. }
  147. }
  148. parent::_afterSave($object);
  149. return $this;
  150. }
  151. /**
  152. * @inheritdoc
  153. */
  154. protected function updateObject(\Magento\Framework\Model\AbstractModel $object)
  155. {
  156. $condition = $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $object->getId());
  157. $data = $this->_prepareDataForSave($object);
  158. unset($data[$this->getIdFieldName()]);
  159. $this->getConnection()->update($this->getMainTable(), $data, $condition);
  160. }
  161. /**
  162. * @inheritdoc
  163. */
  164. protected function saveNewObject(\Magento\Framework\Model\AbstractModel $object)
  165. {
  166. $bind = $this->_prepareDataForSave($object);
  167. unset($bind[$this->getIdFieldName()]);
  168. $this->getConnection()->insert($this->getMainTable(), $bind);
  169. $object->setId($this->getConnection()->lastInsertId($this->getMainTable()));
  170. if ($this->_useIsObjectNew) {
  171. $object->isObjectNew(false);
  172. }
  173. }
  174. /**
  175. * Perform actions after object delete
  176. *
  177. * @param \Magento\Framework\Model\AbstractModel $object
  178. * @return $this
  179. */
  180. protected function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
  181. {
  182. parent::_afterDelete($object);
  183. return $this;
  184. }
  185. }