Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataSetup.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Setup\Module;
9 
13 
18 {
24  private $setupCache;
25 
31  private $_eventManager;
32 
38  private $_logger;
39 
45  private $_migrationFactory;
46 
52  private $filesystem;
53 
60  public function __construct(
61  \Magento\Framework\Module\Setup\Context $context,
62  $connectionName = ModuleDataSetupInterface::DEFAULT_SETUP_CONNECTION
63  ) {
64  parent::__construct($context->getResourceModel(), $connectionName);
65  $this->_eventManager = $context->getEventManager();
66  $this->_logger = $context->getLogger();
67  $this->_migrationFactory = $context->getMigrationFactory();
68  $this->filesystem = $context->getFilesystem();
69  $this->setupCache = new SetupCache();
70  }
71 
75  public function getSetupCache()
76  {
77  return $this->setupCache;
78  }
79 
91  public function getTableRow($table, $idField, $rowId, $field = null, $parentField = null, $parentId = 0)
92  {
93  $table = $this->getTable($table);
94  if (!$this->setupCache->has($table, $parentId, $rowId)) {
95  $connection = $this->getConnection();
96  $bind = ['id_field' => $rowId];
97  $select = $connection->select()
98  ->from($table)
99  ->where($connection->quoteIdentifier($idField) . '= :id_field');
100  if (null !== $parentField) {
101  $select->where($connection->quoteIdentifier($parentField) . '= :parent_id');
102  $bind['parent_id'] = $parentId;
103  }
104  $this->setupCache->setRow($table, $parentId, $rowId, $connection->fetchRow($select, $bind));
105  }
106 
107  return $this->setupCache->get($table, $parentId, $rowId, $field);
108  }
109 
120  public function deleteTableRow($table, $idField, $rowId, $parentField = null, $parentId = 0)
121  {
122  $table = $this->getTable($table);
123  $connection = $this->getConnection();
124  $where = [$connection->quoteIdentifier($idField) . '=?' => $rowId];
125  if (null !== $parentField) {
126  $where[$connection->quoteIdentifier($parentField) . '=?'] = $parentId;
127  }
128 
129  $connection->delete($table, $where);
130 
131  $this->setupCache->remove($table, $parentId, $rowId);
132 
133  return $this;
134  }
135 
149  public function updateTableRow($table, $idField, $rowId, $field, $value = null, $parentField = null, $parentId = 0)
150  {
151  $table = $this->getTable($table);
152  if (is_array($field)) {
153  $data = $field;
154  } else {
155  $data = [$field => $value];
156  }
157 
158  $connection = $this->getConnection();
159  $where = [$connection->quoteIdentifier($idField) . '=?' => $rowId];
160  $connection->update($table, $data, $where);
161 
162  if (is_array($field)) {
163  $oldRow = $this->setupCache->has($table, $parentId, $rowId) ?
164  $this->setupCache->get($table, $parentId, $rowId) :
165  [];
166  $newRowData = array_merge($oldRow, $field);
167  $this->setupCache->setRow($table, $parentId, $rowId, $newRowData);
168  } else {
169  $this->setupCache->setField($table, $parentId, $rowId, $field, $value);
170  }
171 
172  return $this;
173  }
174 
180  public function getEventManager()
181  {
182  return $this->_eventManager;
183  }
184 
190  public function getFilesystem()
191  {
192  return $this->filesystem;
193  }
194 
201  public function createMigrationSetup(array $data = [])
202  {
203  $data['setup'] = $this;
204  return $this->_migrationFactory->create($data);
205  }
206 }
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Module\Setup\Context $context, $connectionName=ModuleDataSetupInterface::DEFAULT_SETUP_CONNECTION)
Definition: DataSetup.php:60
createMigrationSetup(array $data=[])
Definition: DataSetup.php:201
getTableRow($table, $idField, $rowId, $field=null, $parentField=null, $parentId=0)
Definition: DataSetup.php:91
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14
deleteTableRow($table, $idField, $rowId, $parentField=null, $parentId=0)
Definition: DataSetup.php:120
getTable($tableName, $connectionName=ResourceConnection::DEFAULT_CONNECTION)
Definition: Setup.php:120
updateTableRow($table, $idField, $rowId, $field, $value=null, $parentField=null, $parentId=0)
Definition: DataSetup.php:149