Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EntityAbstract.php
Go to the documentation of this file.
1 <?php
7 
13 
21 abstract class EntityAbstract extends AbstractDb
22 {
28  protected $_eventPrefix = 'sales_order_resource';
29 
35  protected $_eventObject = 'resource';
36 
42  protected $_useIsObjectNew = true;
43 
48 
52  protected $attribute;
53 
57  protected $sequenceManager;
58 
67  public function __construct(
68  \Magento\Framework\Model\ResourceModel\Db\Context $context,
69  Snapshot $entitySnapshot,
70  RelationComposite $entityRelationComposite,
73  $connectionName = null
74  ) {
75  $this->attribute = $attribute;
76  $this->sequenceManager = $sequenceManager;
77  if ($connectionName === null) {
78  $connectionName = 'sales';
79  }
80  parent::__construct($context, $entitySnapshot, $entityRelationComposite, $connectionName);
81  }
82 
91  public function saveAttribute(\Magento\Framework\Model\AbstractModel $object, $attribute)
92  {
93  $this->attribute->saveAttribute($object, $attribute);
94  return $this;
95  }
96 
104  protected function _prepareDataForSave(\Magento\Framework\Model\AbstractModel $object)
105  {
106  $data = parent::_prepareDataForTable($object, $this->getMainTable());
107 
108  if (isset($data['updated_at'])) {
109  unset($data['updated_at']);
110  }
111 
112  return $data;
113  }
114 
122  protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
123  {
125  if ($object instanceof EntityInterface && $object->getIncrementId() == null) {
126  $store = $object->getStore();
127  $storeId = $store->getId();
128  if ($storeId === null) {
129  $storeId = $store->getGroup()->getDefaultStoreId();
130  }
131  $object->setIncrementId(
132  $this->sequenceManager->getSequence(
133  $object->getEntityType(),
134  $storeId
135  )->getNextValue()
136  );
137  }
138  parent::_beforeSave($object);
139  return $this;
140  }
141 
148  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
149  {
150  $connection = $this->getConnection();
151  $columns = $connection->describeTable($this->getMainTable());
152 
153  if (isset($columns['created_at'], $columns['updated_at'])) {
154  $select = $connection->select()
155  ->from($this->getMainTable(), ['created_at', 'updated_at'])
156  ->where($this->getIdFieldName() . ' = :entity_id');
157  $row = $connection->fetchRow($select, [':entity_id' => $object->getId()]);
158 
159  if (is_array($row) && isset($row['created_at'], $row['updated_at'])) {
160  $object->setCreatedAt($row['created_at']);
161  $object->setUpdatedAt($row['updated_at']);
162  }
163  }
164 
165  parent::_afterSave($object);
166  return $this;
167  }
168 
172  protected function updateObject(\Magento\Framework\Model\AbstractModel $object)
173  {
174  $condition = $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $object->getId());
175  $data = $this->_prepareDataForSave($object);
176  unset($data[$this->getIdFieldName()]);
177  $this->getConnection()->update($this->getMainTable(), $data, $condition);
178  }
179 
183  protected function saveNewObject(\Magento\Framework\Model\AbstractModel $object)
184  {
185  $bind = $this->_prepareDataForSave($object);
186  unset($bind[$this->getIdFieldName()]);
187  $this->getConnection()->insert($this->getMainTable(), $bind);
188  $object->setId($this->getConnection()->lastInsertId($this->getMainTable()));
189  if ($this->_useIsObjectNew) {
190  $object->isObjectNew(false);
191  }
192  }
193 
200  protected function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
201  {
202  parent::_afterDelete($object);
203  return $this;
204  }
205 }
_afterSave(\Magento\Framework\Model\AbstractModel $object)
$columns
Definition: default.phtml:15
_prepareDataForSave(\Magento\Framework\Model\AbstractModel $object)
saveAttribute(\Magento\Framework\Model\AbstractModel $object, $attribute)
saveNewObject(\Magento\Framework\Model\AbstractModel $object)
_beforeSave(\Magento\Framework\Model\AbstractModel $object)
Definition: AbstractDb.php:653
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, Snapshot $entitySnapshot, RelationComposite $entityRelationComposite, \Magento\Sales\Model\ResourceModel\Attribute $attribute, Manager $sequenceManager, $connectionName=null)
_afterDelete(\Magento\Framework\Model\AbstractModel $object)
$connection
Definition: bulk.php:13
updateObject(\Magento\Framework\Model\AbstractModel $object)