Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Data Structures | Public Member Functions | Data Fields | Protected Attributes
ResourceConnection Class Reference

Data Structures

class  ConnectionFactoryTest
 

Public Member Functions

 __construct (ResourceConfigInterface $resourceConfig, ConnectionFactoryInterface $connectionFactory, DeploymentConfig $deploymentConfig, $tablePrefix='')
 
 getConnection ($resourceName=self::DEFAULT_CONNECTION)
 
 closeConnection ($resourceName=self::DEFAULT_CONNECTION)
 
 getConnectionByName ($connectionName)
 
 getTableName ($modelEntity, $connectionName=self::DEFAULT_CONNECTION)
 
 getTablePlaceholder ($tableName)
 
 getTriggerName ($tableName, $time, $event)
 
 setMappedTableName ($tableName, $mappedName)
 
 getMappedTableName ($tableName)
 
 getIdxName ( $tableName, $fields, $indexType=\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_INDEX)
 
 getFkName ($priTableName, $priColumnName, $refTableName, $refColumnName)
 
 getSchemaName ($resourceName)
 
 getTablePrefix ()
 

Data Fields

const AUTO_UPDATE_ONCE = 0
 
const AUTO_UPDATE_NEVER = -1
 
const AUTO_UPDATE_ALWAYS = 1
 
const DEFAULT_CONNECTION = 'default'
 

Protected Attributes

 $connections = []
 
 $mappedTableNames
 
 $config
 
 $connectionFactory
 
 $tablePrefix
 

Detailed Description

Application provides ability to configure multiple connections to persistent storage. This class provides access to all these connections. @api

Since
100.0.2

Definition at line 18 of file ResourceConnection.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ResourceConfigInterface  $resourceConfig,
ConnectionFactoryInterface  $connectionFactory,
DeploymentConfig  $deploymentConfig,
  $tablePrefix = '' 
)
Parameters
ResourceConfigInterface$resourceConfig
ConnectionFactoryInterface$connectionFactory
DeploymentConfig$deploymentConfig
string$tablePrefix

Definition at line 72 of file ResourceConnection.php.

77  {
78  $this->config = $resourceConfig;
79  $this->connectionFactory = $connectionFactory;
80  $this->deploymentConfig = $deploymentConfig;
81  $this->tablePrefix = $tablePrefix ?: null;
82  }

Member Function Documentation

◆ closeConnection()

closeConnection (   $resourceName = self::DEFAULT_CONNECTION)

Close connection.

Parameters
string$resourceName
Returns
void
Since
100.1.3

Definition at line 106 of file ResourceConnection.php.

107  {
108  if ($resourceName === null) {
109  foreach ($this->connections as $processConnection) {
110  if ($processConnection !== null) {
111  $processConnection->closeConnection();
112  }
113  }
114  $this->connections = [];
115  } else {
116  $processConnectionName = $this->getProcessConnectionName($this->config->getConnectionName($resourceName));
117  if (isset($this->connections[$processConnectionName])) {
118  if ($this->connections[$processConnectionName] !== null) {
119  $this->connections[$processConnectionName]->closeConnection();
120  }
121  $this->connections[$processConnectionName] = null;
122  }
123  }
124  }

◆ getConnection()

getConnection (   $resourceName = self::DEFAULT_CONNECTION)

Retrieve connection to resource specified by $resourceName.

Parameters
string$resourceName
Returns
\Magento\Framework\DB\Adapter\AdapterInterface
Exceptions

Definition at line 92 of file ResourceConnection.php.

93  {
94  $connectionName = $this->config->getConnectionName($resourceName);
95  $connection = $this->getConnectionByName($connectionName);
96  return $connection;
97  }
$connection
Definition: bulk.php:13

◆ getConnectionByName()

getConnectionByName (   $connectionName)

Retrieve connection by $connectionName.

Parameters
string$connectionName
Returns
\Magento\Framework\DB\Adapter\AdapterInterface
Exceptions

Definition at line 133 of file ResourceConnection.php.

134  {
135  $processConnectionName = $this->getProcessConnectionName($connectionName);
136  if (isset($this->connections[$processConnectionName])) {
137  return $this->connections[$processConnectionName];
138  }
139 
140  $connectionConfig = $this->deploymentConfig->get(
142  );
143 
144  if ($connectionConfig) {
145  $connection = $this->connectionFactory->create($connectionConfig);
146  } else {
147  throw new \DomainException('Connection "' . $connectionName . '" is not defined');
148  }
149 
150  $this->connections[$processConnectionName] = $connection;
151  return $connection;
152  }
$connection
Definition: bulk.php:13

◆ getFkName()

getFkName (   $priTableName,
  $priColumnName,
  $refTableName,
  $refColumnName 
)

Retrieve 32bit UNIQUE HASH for a Table foreign key.

Parameters
string$priTableNamethe target table name
string$priColumnNamethe target table column name
string$refTableNamethe reference table name
string$refColumnNamethe reference table column name
Returns
string

Definition at line 283 of file ResourceConnection.php.

284  {
285  return $this->getConnection()->getForeignKeyName(
286  $this->getTableName($priTableName),
287  $priColumnName,
288  $this->getTableName($refTableName),
289  $refColumnName
290  );
291  }
getConnection($resourceName=self::DEFAULT_CONNECTION)
getTableName($modelEntity, $connectionName=self::DEFAULT_CONNECTION)

◆ getIdxName()

getIdxName (   $tableName,
  $fields,
  $indexType = \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_INDEX 
)

Retrieve 32bit UNIQUE HASH for a Table index.

Parameters
string$tableName
string|string[]$fields
string$indexType
Returns
string

Definition at line 261 of file ResourceConnection.php.

265  {
266  return $this->getConnection()
267  ->getIndexName(
268  $this->getTableName($tableName),
269  $fields,
270  $indexType
271  );
272  }
getConnection($resourceName=self::DEFAULT_CONNECTION)
$tableName
Definition: trigger.php:13
$fields
Definition: details.phtml:14
getTableName($modelEntity, $connectionName=self::DEFAULT_CONNECTION)

◆ getMappedTableName()

getMappedTableName (   $tableName)

Get mapped table name.

Parameters
string$tableName
Returns
bool|string

Definition at line 244 of file ResourceConnection.php.

245  {
246  if (isset($this->mappedTableNames[$tableName])) {
247  return $this->mappedTableNames[$tableName];
248  } else {
249  return false;
250  }
251  }
$tableName
Definition: trigger.php:13

◆ getSchemaName()

getSchemaName (   $resourceName)

Retrieve db name.

That name can be needed, when we do request in information_schema to identify db.

Parameters
string$resourceName
Returns
string

Definition at line 301 of file ResourceConnection.php.

302  {
303  return $this->deploymentConfig->get(
305  '/' .
306  $resourceName .
307  '/dbname'
308  );
309  }

◆ getTableName()

getTableName (   $modelEntity,
  $connectionName = self::DEFAULT_CONNECTION 
)

Get resource table name, validated by db adapter.

Parameters
string|string[]$modelEntity
string$connectionName
Returns
string @api

Definition at line 173 of file ResourceConnection.php.

174  {
175  $tableSuffix = null;
176  if (is_array($modelEntity)) {
177  list($modelEntity, $tableSuffix) = $modelEntity;
178  }
179 
180  $tableName = $modelEntity;
181 
182  $mappedTableName = $this->getMappedTableName($tableName);
183  if ($mappedTableName) {
184  $tableName = $mappedTableName;
185  } else {
186  $tablePrefix = $this->getTablePrefix();
187  if ($tablePrefix && strpos($tableName, $tablePrefix) !== 0) {
189  }
190  }
191 
192  if ($tableSuffix) {
193  $tableName .= '_' . $tableSuffix;
194  }
195  return $this->getConnection($connectionName)->getTableName($tableName);
196  }
getConnection($resourceName=self::DEFAULT_CONNECTION)
$tableName
Definition: trigger.php:13

◆ getTablePlaceholder()

getTablePlaceholder (   $tableName)

Gets table placeholder by table name.

Parameters
string$tableName
Returns
string
Since
100.1.0

Definition at line 205 of file ResourceConnection.php.

206  {
207  $tableName = preg_replace('/^' . preg_quote($this->getTablePrefix()) . '_/', '', $tableName);
208  return $tableName;
209  }
$tableName
Definition: trigger.php:13

◆ getTablePrefix()

getTablePrefix ( )

Get table prefix.

Returns
string

Definition at line 316 of file ResourceConnection.php.

317  {
318  if ($this->tablePrefix !== null) {
319  return $this->tablePrefix;
320  }
321 
322  return (string) $this->deploymentConfig->get(
324  );
325  }

◆ getTriggerName()

getTriggerName (   $tableName,
  $time,
  $event 
)

Build a trigger name.

Parameters
string$tableNameThe table that is the subject of the trigger
string$timeEither "before" or "after"
string$eventThe DB level event which activates the trigger, i.e. "update" or "insert"
Returns
string

Definition at line 219 of file ResourceConnection.php.

220  {
221  return $this->getConnection()->getTriggerName($tableName, $time, $event);
222  }
getConnection($resourceName=self::DEFAULT_CONNECTION)
$tableName
Definition: trigger.php:13

◆ setMappedTableName()

setMappedTableName (   $tableName,
  $mappedName 
)

Set mapped table name.

Parameters
string$tableName
string$mappedName
Returns
$this @codeCoverageIgnore

Definition at line 232 of file ResourceConnection.php.

233  {
234  $this->mappedTableNames[$tableName] = $mappedName;
235  return $this;
236  }
$tableName
Definition: trigger.php:13

Field Documentation

◆ $config

$config
protected

Definition at line 47 of file ResourceConnection.php.

◆ $connectionFactory

$connectionFactory
protected

Definition at line 54 of file ResourceConnection.php.

◆ $connections

$connections = []
protected

Definition at line 33 of file ResourceConnection.php.

◆ $mappedTableNames

$mappedTableNames
protected

Definition at line 40 of file ResourceConnection.php.

◆ $tablePrefix

$tablePrefix
protected

Definition at line 64 of file ResourceConnection.php.

◆ AUTO_UPDATE_ALWAYS

const AUTO_UPDATE_ALWAYS = 1

Definition at line 24 of file ResourceConnection.php.

◆ AUTO_UPDATE_NEVER

const AUTO_UPDATE_NEVER = -1

Definition at line 22 of file ResourceConnection.php.

◆ AUTO_UPDATE_ONCE

const AUTO_UPDATE_ONCE = 0

Definition at line 20 of file ResourceConnection.php.

◆ DEFAULT_CONNECTION

const DEFAULT_CONNECTION = 'default'

Definition at line 26 of file ResourceConnection.php.


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