Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
DataSetup Class Reference
Inheritance diagram for DataSetup:
Setup ModuleDataSetupInterface SetupInterface SetupInterface

Public Member Functions

 __construct (\Magento\Framework\Module\Setup\Context $context, $connectionName=ModuleDataSetupInterface::DEFAULT_SETUP_CONNECTION)
 
 getSetupCache ()
 
 getTableRow ($table, $idField, $rowId, $field=null, $parentField=null, $parentId=0)
 
 deleteTableRow ($table, $idField, $rowId, $parentField=null, $parentId=0)
 
 updateTableRow ($table, $idField, $rowId, $field, $value=null, $parentField=null, $parentId=0)
 
 getEventManager ()
 
 getFilesystem ()
 
 createMigrationSetup (array $data=[])
 
- Public Member Functions inherited from Setup
 __construct (\Magento\Framework\App\ResourceConnection $resource, $connectionName=ModuleDataSetupInterface::DEFAULT_SETUP_CONNECTION)
 
 getConnection ($connectionName=null)
 
 setTable ($tableName, $realTableName)
 
 getTablePlaceholder ($tableName)
 
 getTable ($tableName, $connectionName=ResourceConnection::DEFAULT_CONNECTION)
 
 tableExists ($table, $connectionName=ResourceConnection::DEFAULT_CONNECTION)
 
 run ($sql)
 
 startSetup ()
 
 endSetup ()
 
- Public Member Functions inherited from SetupInterface
 getConnection ()
 
 getTable ($tableName)
 
 tableExists ($table)
 

Additional Inherited Members

- Data Fields inherited from ModuleDataSetupInterface
const DEFAULT_SETUP_CONNECTION = 'default_setup'
 
const VERSION_COMPARE_EQUAL = 0
 
const VERSION_COMPARE_LOWER = -1
 
const VERSION_COMPARE_GREATER = 1
 
const TYPE_DATA_INSTALL = 'data-install'
 
const TYPE_DATA_UPGRADE = 'data-upgrade'
 

Detailed Description

@api

Definition at line 17 of file DataSetup.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Module\Setup\Context  $context,
  $connectionName = ModuleDataSetupInterface::DEFAULT_SETUP_CONNECTION 
)

Init

Parameters
\Magento\Framework\Module\Setup\Context$context
string$connectionName

Definition at line 60 of file DataSetup.php.

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  }

Member Function Documentation

◆ createMigrationSetup()

createMigrationSetup ( array  $data = [])

Create migration setup

Parameters
array$data
Returns
\Magento\Framework\Module\Setup\Migration

Implements ModuleDataSetupInterface.

Definition at line 201 of file DataSetup.php.

202  {
203  $data['setup'] = $this;
204  return $this->_migrationFactory->create($data);
205  }

◆ deleteTableRow()

deleteTableRow (   $table,
  $idField,
  $rowId,
  $parentField = null,
  $parentId = 0 
)

Delete table row

Parameters
string$table
string$idField
string | int$rowId
null | string$parentField
int | string$parentId
Returns
$this

Implements ModuleDataSetupInterface.

Definition at line 120 of file DataSetup.php.

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  }
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14
getTable($tableName, $connectionName=ResourceConnection::DEFAULT_CONNECTION)
Definition: Setup.php:120

◆ getEventManager()

getEventManager ( )

Gets event manager

Returns
\Magento\Framework\Event\ManagerInterface

Implements ModuleDataSetupInterface.

Definition at line 180 of file DataSetup.php.

181  {
182  return $this->_eventManager;
183  }

◆ getFilesystem()

getFilesystem ( )

Gets filesystem

Returns
\Magento\Framework\Filesystem

Implements ModuleDataSetupInterface.

Definition at line 190 of file DataSetup.php.

191  {
192  return $this->filesystem;
193  }
$filesystem

◆ getSetupCache()

getSetupCache ( )

{Gets setup cache

Returns
DataCacheInterface
}

Implements ModuleDataSetupInterface.

Definition at line 75 of file DataSetup.php.

76  {
77  return $this->setupCache;
78  }

◆ getTableRow()

getTableRow (   $table,
  $idField,
  $rowId,
  $field = null,
  $parentField = null,
  $parentId = 0 
)

Retrieve row or field from table by id or string and parent id

Parameters
string$table
string$idField
string | integer$rowId
string | null$field
string | null$parentField
string | integer$parentId
Returns
mixed

Implements ModuleDataSetupInterface.

Definition at line 91 of file DataSetup.php.

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  }
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14
getTable($tableName, $connectionName=ResourceConnection::DEFAULT_CONNECTION)
Definition: Setup.php:120

◆ updateTableRow()

updateTableRow (   $table,
  $idField,
  $rowId,
  $field,
  $value = null,
  $parentField = null,
  $parentId = 0 
)

Update one or more fields of table row

Parameters
string$table
string$idField
string | integer$rowId
string | array$field
mixed | null$value
string$parentField
string | integer$parentId
Returns
$this @SuppressWarnings(PHPMD.UnusedFormalParameter)

Implements ModuleDataSetupInterface.

Definition at line 149 of file DataSetup.php.

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  }
$value
Definition: gender.phtml:16
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14
getTable($tableName, $connectionName=ResourceConnection::DEFAULT_CONNECTION)
Definition: Setup.php:120

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