Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StaticAttributeStrategy.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Eav\Model\Config as EavConfig;
12 
20 {
24  private $resourceConnection;
25 
29  private $aliasResolver;
30 
34  private $eavConfig;
35 
41  public function __construct(
42  \Magento\Framework\App\ResourceConnection $resourceConnection,
43  EavConfig $eavConfig,
44  AliasResolver $aliasResolver
45  ) {
46  $this->resourceConnection = $resourceConnection;
47  $this->eavConfig = $eavConfig;
48  $this->aliasResolver = $aliasResolver;
49  }
50 
54  public function apply(
55  \Magento\Framework\Search\Request\FilterInterface $filter,
56  \Magento\Framework\DB\Select $select
57  ) {
58  $attribute = $this->getAttributeByCode($filter->getField());
59  $alias = $this->aliasResolver->getAlias($filter);
60  $mainTableAlias = $this->extractTableAliasFromSelect($select);
61 
62  $select->joinInner(
63  [$alias => $attribute->getBackendTable()],
64  sprintf('%s.entity_id = ', $mainTableAlias)
65  . $this->resourceConnection->getConnection()->quoteIdentifier("$alias.entity_id"),
66  []
67  );
68 
69  return true;
70  }
71 
77  private function getAttributeByCode($field)
78  {
79  return $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $field);
80  }
81 
88  private function extractTableAliasFromSelect(Select $select)
89  {
90  $fromArr = array_filter(
91  $select->getPart(Select::FROM),
92  function ($fromPart) {
93  return $fromPart['joinType'] === Select::FROM;
94  }
95  );
96 
97  return $fromArr ? array_keys($fromArr)[0] : null;
98  }
99 }
apply(\Magento\Framework\Search\Request\FilterInterface $filter, \Magento\Framework\DB\Select $select)
__construct(\Magento\Framework\App\ResourceConnection $resourceConnection, EavConfig $eavConfig, AliasResolver $aliasResolver)
const FROM
Definition: Select.php:49
if(!trim($html)) $alias
Definition: details.phtml:20