Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataCategoryUrlRewriteDatabaseMap.php
Go to the documentation of this file.
1 <?php
7 
12 
17 {
18  const ENTITY_TYPE = 'category';
19 
23  private $createdTableAdapters = [];
24 
28  private $hashMapPool;
29 
33  private $connection;
34 
38  private $temporaryTableService;
39 
45  public function __construct(
46  ResourceConnection $connection,
47  HashMapPool $hashMapPool,
48  TemporaryTableService $temporaryTableService
49  ) {
50  $this->connection = $connection;
51  $this->hashMapPool = $hashMapPool;
52  $this->temporaryTableService = $temporaryTableService;
53  }
54 
61  private function generateTableAdapter($categoryId)
62  {
63  if (!isset($this->createdTableAdapters[$categoryId])) {
64  $this->createdTableAdapters[$categoryId] = $this->generateData($categoryId);
65  }
66  }
67 
75  private function generateData($categoryId)
76  {
77  $urlRewritesConnection = $this->connection->getConnection();
78  $select = $urlRewritesConnection->select()
79  ->from(
80  ['e' => $this->connection->getTableName('url_rewrite')],
81  ['e.*', 'hash_key' => new \Zend_Db_Expr(
82  "CONCAT(e.store_id,'" . MergeDataProvider::SEPARATOR . "', e.entity_id)"
83  )
84  ]
85  )
86  ->where('entity_type = ?', self::ENTITY_TYPE)
87  ->where(
88  $urlRewritesConnection->prepareSqlCondition(
89  'entity_id',
90  [
91  'in' => array_merge(
92  $this->hashMapPool->getDataMap(DataCategoryUsedInProductsHashMap::class, $categoryId)
93  ->getAllData($categoryId),
94  $this->hashMapPool->getDataMap(DataCategoryHashMap::class, $categoryId)
95  ->getAllData($categoryId)
96  )
97  ]
98  )
99  );
100  $mapName = $this->temporaryTableService->createFromSelect(
101  $select,
102  $this->connection->getConnection(),
103  [
104  'PRIMARY' => ['url_rewrite_id'],
105  'HASHKEY_ENTITY_STORE' => ['hash_key'],
106  'ENTITY_STORE' => ['entity_id', 'store_id']
107  ]
108  );
109  return $mapName;
110  }
111 
115  public function destroyTableAdapter($categoryId)
116  {
117  $this->hashMapPool->resetMap(DataCategoryUsedInProductsHashMap::class, $categoryId);
118  $this->hashMapPool->resetMap(DataCategoryHashMap::class, $categoryId);
119  if (isset($this->createdTableAdapters[$categoryId])) {
120  $this->temporaryTableService->dropTable($this->createdTableAdapters[$categoryId]);
121  unset($this->createdTableAdapters[$categoryId]);
122  }
123  }
124 
132  public function getData($categoryId, $key)
133  {
134  $this->generateTableAdapter($categoryId);
135  $urlRewritesConnection = $this->connection->getConnection();
136  $select = $urlRewritesConnection->select()->from(['e' => $this->createdTableAdapters[$categoryId]]);
137  if (strlen($key) > 0) {
138  $select->where('hash_key = ?', $key);
139  }
140 
141  return $urlRewritesConnection->fetchAll($select);
142  }
143 }
__construct(ResourceConnection $connection, HashMapPool $hashMapPool, TemporaryTableService $temporaryTableService)
$connection
Definition: bulk.php:13