Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractResource.php
Go to the documentation of this file.
1 <?php
7 
12 
19 abstract class AbstractResource
20 {
25  protected $serializer;
26 
30  protected $_logger;
31 
35  public function __construct()
36  {
40  $this->_construct();
41  }
42 
48  abstract protected function _construct();
49 
55  abstract public function getConnection();
56 
63  public function beginTransaction()
64  {
65  $this->getConnection()->beginTransaction();
66  return $this;
67  }
68 
76  public function addCommitCallback($callback)
77  {
78  CallbackPool::attach(spl_object_hash($this->getConnection()), $callback);
79  return $this;
80  }
81 
88  public function commit()
89  {
90  $this->getConnection()->commit();
94  if ($this->getConnection()->getTransactionLevel() === 0) {
95  $callbacks = CallbackPool::get(spl_object_hash($this->getConnection()));
96  try {
97  foreach ($callbacks as $callback) {
98  call_user_func($callback);
99  }
100  } catch (\Exception $e) {
101  $this->getLogger()->critical($e);
102  }
103  }
104  return $this;
105  }
106 
113  public function rollBack()
114  {
115  $this->getConnection()->rollBack();
116  CallbackPool::clear(spl_object_hash($this->getConnection()));
117  return $this;
118  }
119 
129  protected function _serializeField(DataObject $object, $field, $defaultValue = null, $unsetEmpty = false)
130  {
131  $value = $object->getData($field);
132  if (empty($value) && $unsetEmpty) {
133  $object->unsetData($field);
134  } else {
135  $object->setData($field, $this->getSerializer()->serialize($value ?: $defaultValue));
136  }
137 
138  return $this;
139  }
140 
149  protected function _unserializeField(DataObject $object, $field, $defaultValue = null)
150  {
151  $value = $object->getData($field);
152  if ($value) {
153  $value = $this->getSerializer()->unserialize($object->getData($field));
154  if (empty($value)) {
155  $object->setData($field, $defaultValue);
156  } else {
157  $object->setData($field, $value);
158  }
159  } else {
160  $object->setData($field, $defaultValue);
161  }
162  }
163 
171  protected function _prepareDataForTable(DataObject $object, $table)
172  {
173  $data = [];
174  $fields = $this->getConnection()->describeTable($table);
175  foreach (array_keys($fields) as $field) {
176  if ($object->hasData($field)) {
177  $fieldValue = $object->getData($field);
178  if ($fieldValue instanceof \Zend_Db_Expr) {
179  $data[$field] = $fieldValue;
180  } else {
181  if (null !== $fieldValue) {
182  $fieldValue = $this->_prepareTableValueForSave($fieldValue, $fields[$field]['DATA_TYPE']);
183  $data[$field] = $this->getConnection()->prepareColumnValue($fields[$field], $fieldValue);
184  } elseif (!empty($fields[$field]['NULLABLE'])) {
185  $data[$field] = null;
186  }
187  }
188  }
189  }
190  return $data;
191  }
192 
201  {
202  $type = strtolower($type);
203  if ($type == 'decimal' || $type == 'numeric' || $type == 'float') {
205  \Magento\Framework\Locale\FormatInterface::class
206  )->getNumber(
207  $value
208  );
209  }
210  return $value;
211  }
212 
219  {
220  return null;
221  }
222 
230  protected function _getColumnsForEntityLoad(\Magento\Framework\Model\AbstractModel $object, $tableName)
231  {
232  $fieldsetColumns = $object->getFieldset();
233  if (!empty($fieldsetColumns)) {
234  $connection = $this->getConnection();
235  if ($connection instanceof \Magento\Framework\DB\Adapter\AdapterInterface) {
236  $entityTableColumns = $connection->describeTable($tableName);
237  $columns = array_intersect($fieldsetColumns, array_keys($entityTableColumns));
238  }
239  }
240  if (empty($columns)) {
242  $columns = empty($fieldsetColumns) ? '*' : [$object->getIdFieldName()];
243  }
244  return $columns;
245  }
246 
254  protected function getSerializer()
255  {
256  if (null === $this->serializer) {
257  $this->serializer = ObjectManager::getInstance()->get(Json::class);
258  }
259  return $this->serializer;
260  }
261 
268  private function getLogger()
269  {
270  if (null === $this->_logger) {
271  $this->_logger = ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
272  }
273  return $this->_logger;
274  }
275 }
_getColumnsForEntityLoad(\Magento\Framework\Model\AbstractModel $object, $tableName)
$tableName
Definition: trigger.php:13
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$fields
Definition: details.phtml:14
_unserializeField(DataObject $object, $field, $defaultValue=null)
$columns
Definition: default.phtml:15
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
static attach($hashKey, $callback)
setData($key, $value=null)
Definition: DataObject.php:72
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14
_serializeField(DataObject $object, $field, $defaultValue=null, $unsetEmpty=false)