Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MigrateDataFromAnotherTable.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
15 
20 {
24  const MATCH_PATTERN = '/migrateDataFromAnotherTable\(([^\)]+)\)/';
25 
29  private $resourceConnection;
30 
36  public function __construct(ResourceConnection $resourceConnection)
37  {
38  $this->resourceConnection = $resourceConnection;
39  }
40 
44  public function isApplicable(string $statement) : bool
45  {
46  return (bool) preg_match(self::MATCH_PATTERN, $statement);
47  }
48 
52  public function getCallback(ElementHistory $tableHistory) : callable
53  {
55  $table = $tableHistory->getNew();
56  preg_match(self::MATCH_PATTERN, $table->getOnCreate(), $matches);
57  return function () use ($table, $matches) {
58  $tableName = $table->getName();
59  $oldTableName = $this->resourceConnection->getTableName($matches[1]);
60  $adapter = $this->resourceConnection->getConnection($table->getResource());
61  $select = $adapter->select()->from($oldTableName);
62  $adapter->query($adapter->insertFromSelect($select, $tableName));
63  };
64  }
65 }
$tableName
Definition: trigger.php:13
$adapter
Definition: webapi_user.php:16
$table
Definition: trigger.php:14