Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
JoinAttributeProcessor.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 
18 {
22  private $eavConfig;
23 
27  private $metadataPool;
28 
32  private $resource;
33 
37  private $connectionName;
38 
45  public function __construct(
46  \Magento\Eav\Model\Config $eavConfig,
47  \Magento\Framework\EntityManager\MetadataPool $metadataPool,
48  \Magento\Framework\App\ResourceConnection $resource,
49  $connectionName = 'indexer'
50  ) {
51  $this->eavConfig = $eavConfig;
52  $this->metadataPool = $metadataPool;
53  $this->resource = $resource;
54  $this->connectionName = $connectionName;
55  }
56 
65  public function process(Select $select, $attributeCode, $attributeValue = null): \Zend_Db_Expr
66  {
67  $attribute = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);
68  $attributeId = $attribute->getAttributeId();
69  $attributeTable = $attribute->getBackend()->getTable();
70  $connection = $this->resource->getConnection($this->connectionName);
71  $joinType = $attributeValue !== null ? 'join' : 'joinLeft';
72  $productIdField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
73 
74  if ($attribute->isScopeGlobal()) {
75  $alias = 'ta_' . $attributeCode;
76  $select->{$joinType}(
77  [$alias => $attributeTable],
78  "{$alias}.{$productIdField} = e.{$productIdField} AND {$alias}.attribute_id = {$attributeId}" .
79  " AND {$alias}.store_id = 0",
80  []
81  );
82  $whereExpression = new Expression("{$alias}.value");
83  } else {
84  $dAlias = 'tad_' . $attributeCode;
85  $sAlias = 'tas_' . $attributeCode;
86 
87  $select->{$joinType}(
88  [$dAlias => $attributeTable],
89  "{$dAlias}.{$productIdField} = e.{$productIdField} AND {$dAlias}.attribute_id = {$attributeId}" .
90  " AND {$dAlias}.store_id = 0",
91  []
92  );
93  $select->joinLeft(
94  [$sAlias => $attributeTable],
95  "{$sAlias}.{$productIdField} = e.{$productIdField} AND {$sAlias}.attribute_id = {$attributeId}" .
96  " AND {$sAlias}.store_id = cwd.default_store_id",
97  []
98  );
99  $whereExpression = $connection->getCheckSql(
100  $connection->getIfNullSql("{$sAlias}.value_id", -1) . ' > 0',
101  "{$sAlias}.value",
102  "{$dAlias}.value"
103  );
104  }
105 
106  if ($attributeValue !== null) {
107  $select->where("{$whereExpression} = ?", $attributeValue);
108  }
109 
110  return $whereExpression;
111  }
112 }
$resource
Definition: bulk.php:12
__construct(\Magento\Eav\Model\Config $eavConfig, \Magento\Framework\EntityManager\MetadataPool $metadataPool, \Magento\Framework\App\ResourceConnection $resource, $connectionName='indexer')
$attributeCode
Definition: extend.phtml:12
if(!trim($html)) $alias
Definition: details.phtml:20
$connection
Definition: bulk.php:13