Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Transaction Class Reference

Public Member Functions

 addObject (\Magento\Framework\Model\AbstractModel $object, $alias='')
 
 addCommitCallback ($callback)
 
 save ()
 
 delete ()
 

Protected Member Functions

 _startTransaction ()
 
 _commitTransaction ()
 
 _rollbackTransaction ()
 
 _runCallbacks ()
 

Protected Attributes

 $_objects = []
 
 $_objectsByAlias = []
 
 $_beforeCommitCallbacks = []
 

Detailed Description

Todo:
need collect connection by name

Definition at line 13 of file Transaction.php.

Member Function Documentation

◆ _commitTransaction()

_commitTransaction ( )
protected

Commit transaction for all resources

Returns
$this

Definition at line 54 of file Transaction.php.

55  {
56  foreach ($this->_objects as $object) {
57  $object->getResource()->commit();
58  }
59  return $this;
60  }

◆ _rollbackTransaction()

_rollbackTransaction ( )
protected

Rollback transaction

Returns
$this

Definition at line 67 of file Transaction.php.

68  {
69  foreach ($this->_objects as $object) {
70  $object->getResource()->rollBack();
71  }
72  return $this;
73  }

◆ _runCallbacks()

_runCallbacks ( )
protected

Run all configured object callbacks

Returns
$this

Definition at line 80 of file Transaction.php.

81  {
82  foreach ($this->_beforeCommitCallbacks as $callback) {
83  call_user_func($callback);
84  }
85  return $this;
86  }

◆ _startTransaction()

_startTransaction ( )
protected

Begin transaction for all involved object resources

Returns
$this

Definition at line 41 of file Transaction.php.

42  {
43  foreach ($this->_objects as $object) {
44  $object->getResource()->beginTransaction();
45  }
46  return $this;
47  }

◆ addCommitCallback()

addCommitCallback (   $callback)

Add callback function which will be called before commit transactions

Parameters
callback$callback
Returns
$this

Definition at line 110 of file Transaction.php.

111  {
112  $this->_beforeCommitCallbacks[] = $callback;
113  return $this;
114  }

◆ addObject()

addObject ( \Magento\Framework\Model\AbstractModel  $object,
  $alias = '' 
)

Adding object for using in transaction

Parameters
\Magento\Framework\Model\AbstractModel$object
string$alias
Returns
$this

Definition at line 95 of file Transaction.php.

96  {
97  $this->_objects[] = $object;
98  if (!empty($alias)) {
99  $this->_objectsByAlias[$alias] = $object;
100  }
101  return $this;
102  }
if(!trim($html)) $alias
Definition: details.phtml:20

◆ delete()

delete ( )

Initialize objects delete transaction

Returns
$this
Exceptions

Definition at line 159 of file Transaction.php.

160  {
161  $this->_startTransaction();
162  $error = false;
163 
164  try {
165  foreach ($this->_objects as $object) {
166  $object->delete();
167  }
168  } catch (\Exception $e) {
169  $error = $e;
170  }
171 
172  if ($error === false) {
173  try {
174  $this->_runCallbacks();
175  } catch (\Exception $e) {
176  $error = $e;
177  }
178  }
179 
180  if ($error) {
181  $this->_rollbackTransaction();
182  throw $error;
183  } else {
184  $this->_commitTransaction();
185  }
186  return $this;
187  }

◆ save()

save ( )

Initialize objects save transaction

Returns
$this
Exceptions

Definition at line 122 of file Transaction.php.

123  {
124  $this->_startTransaction();
125  $error = false;
126 
127  try {
128  foreach ($this->_objects as $object) {
129  $object->save();
130  }
131  } catch (\Exception $e) {
132  $error = $e;
133  }
134 
135  if ($error === false) {
136  try {
137  $this->_runCallbacks();
138  } catch (\Exception $e) {
139  $error = $e;
140  }
141  }
142 
143  if ($error) {
144  $this->_rollbackTransaction();
145  throw $error;
146  } else {
147  $this->_commitTransaction();
148  }
149 
150  return $this;
151  }

Field Documentation

◆ $_beforeCommitCallbacks

$_beforeCommitCallbacks = []
protected

Definition at line 34 of file Transaction.php.

◆ $_objects

$_objects = []
protected

Definition at line 20 of file Transaction.php.

◆ $_objectsByAlias

$_objectsByAlias = []
protected

Definition at line 27 of file Transaction.php.


The documentation for this class was generated from the following file: