Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ApplyNameAttributeJoin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\Eav\Model\Config as EavConfig;
18 
23 {
27  private $resourceConnection;
28 
32  private $eavConfig;
33 
37  private $storeManager;
38 
42  private $metadataPool;
43 
50  public function __construct(
51  ResourceConnection $resourceConnection,
52  EavConfig $eavConfig,
53  StoreManagerInterface $storeManager,
54  MetadataPool $metadataPool
55  ) {
56  $this->resourceConnection = $resourceConnection;
57  $this->storeManager = $storeManager;
58  $this->eavConfig = $eavConfig;
59  $this->metadataPool = $metadataPool;
60  }
61 
67  public function execute(Select $select)
68  {
69  $storeId = $this->storeManager->getStore()->getId();
70  $connection = $this->resourceConnection->getConnection();
71  $valueCondition = 'at_name.value';
72  $tableName = $this->resourceConnection->getTableName('catalog_product_entity_varchar');
73 
75  $select->join(
76  ['at_name_default' => $tableName],
77  $this->getConditionByAliasAndStoreId(Store::DEFAULT_STORE_ID, 'at_name_default'),
78  []
79  );
80  $valueCondition = $connection->getCheckSql(
81  'at_name.value_id > 0',
82  'at_name.value',
83  'at_name_default.value'
84  );
85  }
86 
87  $select->joinLeft(
88  ['at_name' => $tableName],
89  $this->getConditionByAliasAndStoreId((int)$storeId, 'at_name'),
90  ['name' => $valueCondition]
91  );
92  }
93 
100  private function getConditionByAliasAndStoreId(int $storeId, string $alias): string
101  {
102  $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
103  $linkField = $metadata->getLinkField();
104  $attributeId = $this->eavConfig->getAttribute(Product::ENTITY, ProductInterface::NAME)->getAttributeId();
105  $connection = $this->resourceConnection->getConnection();
106 
107  return implode(
108  [
109  $alias . '.' . $linkField . ' = product.' . $linkField,
110  $connection->prepareSqlCondition($alias . '.store_id', $storeId),
111  $connection->prepareSqlCondition($alias . '.attribute_id', $attributeId),
112  ],
113  ' ' . Select::SQL_AND . ' '
114  );
115  }
116 }
$tableName
Definition: trigger.php:13
$storeManager
__construct(ResourceConnection $resourceConnection, EavConfig $eavConfig, StoreManagerInterface $storeManager, MetadataPool $metadataPool)
const SQL_AND
Definition: Select.php:77
if(!trim($html)) $alias
Definition: details.phtml:20
$connection
Definition: bulk.php:13