Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ResourceConnection.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\App;
7 
11 
19 {
20  const AUTO_UPDATE_ONCE = 0;
21 
22  const AUTO_UPDATE_NEVER = -1;
23 
24  const AUTO_UPDATE_ALWAYS = 1;
25 
26  const DEFAULT_CONNECTION = 'default';
27 
33  protected $connections = [];
34 
40  protected $mappedTableNames;
41 
47  protected $config;
48 
54  protected $connectionFactory;
55 
59  private $deploymentConfig;
60 
64  protected $tablePrefix;
65 
72  public function __construct(
73  ResourceConfigInterface $resourceConfig,
75  DeploymentConfig $deploymentConfig,
76  $tablePrefix = ''
77  ) {
78  $this->config = $resourceConfig;
79  $this->connectionFactory = $connectionFactory;
80  $this->deploymentConfig = $deploymentConfig;
81  $this->tablePrefix = $tablePrefix ?: null;
82  }
83 
92  public function getConnection($resourceName = self::DEFAULT_CONNECTION)
93  {
94  $connectionName = $this->config->getConnectionName($resourceName);
95  $connection = $this->getConnectionByName($connectionName);
96  return $connection;
97  }
98 
106  public function closeConnection($resourceName = self::DEFAULT_CONNECTION)
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  }
125 
133  public function getConnectionByName($connectionName)
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  }
153 
160  private function getProcessConnectionName($connectionName)
161  {
162  return $connectionName . '_process_' . getmypid();
163  }
164 
173  public function getTableName($modelEntity, $connectionName = self::DEFAULT_CONNECTION)
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  }
197 
206  {
207  $tableName = preg_replace('/^' . preg_quote($this->getTablePrefix()) . '_/', '', $tableName);
208  return $tableName;
209  }
210 
219  public function getTriggerName($tableName, $time, $event)
220  {
221  return $this->getConnection()->getTriggerName($tableName, $time, $event);
222  }
223 
232  public function setMappedTableName($tableName, $mappedName)
233  {
234  $this->mappedTableNames[$tableName] = $mappedName;
235  return $this;
236  }
237 
245  {
246  if (isset($this->mappedTableNames[$tableName])) {
247  return $this->mappedTableNames[$tableName];
248  } else {
249  return false;
250  }
251  }
252 
261  public function getIdxName(
262  $tableName,
263  $fields,
264  $indexType = \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_INDEX
265  ) {
266  return $this->getConnection()
267  ->getIndexName(
268  $this->getTableName($tableName),
269  $fields,
270  $indexType
271  );
272  }
273 
283  public function getFkName($priTableName, $priColumnName, $refTableName, $refColumnName)
284  {
285  return $this->getConnection()->getForeignKeyName(
286  $this->getTableName($priTableName),
287  $priColumnName,
288  $this->getTableName($refTableName),
289  $refColumnName
290  );
291  }
292 
301  public function getSchemaName($resourceName)
302  {
303  return $this->deploymentConfig->get(
305  '/' .
306  $resourceName .
307  '/dbname'
308  );
309  }
310 
316  public function getTablePrefix()
317  {
318  if ($this->tablePrefix !== null) {
319  return $this->tablePrefix;
320  }
321 
322  return (string) $this->deploymentConfig->get(
324  );
325  }
326 }
getConnection($resourceName=self::DEFAULT_CONNECTION)
$tableName
Definition: trigger.php:13
getIdxName( $tableName, $fields, $indexType=\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_INDEX)
$fields
Definition: details.phtml:14
$deploymentConfig
getTableName($modelEntity, $connectionName=self::DEFAULT_CONNECTION)
__construct(ResourceConfigInterface $resourceConfig, ConnectionFactoryInterface $connectionFactory, DeploymentConfig $deploymentConfig, $tablePrefix='')
closeConnection($resourceName=self::DEFAULT_CONNECTION)
$connection
Definition: bulk.php:13
getFkName($priTableName, $priColumnName, $refTableName, $refColumnName)