Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 
14 {
20  protected $_entityTypeId;
21 
27  protected $_storeId = null;
28 
34  protected $_eavConfig;
35 
41  protected $_storeManager;
42 
49  public function __construct(
50  \Magento\Framework\Model\ResourceModel\Db\Context $context,
52  \Magento\Eav\Model\Config $eavConfig,
53  $connectionName = null
54  ) {
55  $this->_storeManager = $storeManager;
56  $this->_eavConfig = $eavConfig;
57  parent::__construct($context, $connectionName);
58  }
59 
65  protected function _construct()
66  {
67  $this->_init('eav_attribute', 'attribute_id');
68  }
69 
76  public function setStoreId($storeId)
77  {
78  $this->_storeId = (int)$storeId;
79  return $this;
80  }
81 
88  public function getStoreId()
89  {
90  if ($this->_storeId === null) {
91  $this->_storeId = (int)$this->_storeManager->getStore()->getId();
92  }
93  return $this->_storeId;
94  }
95 
101  public function getEntityTypeId()
102  {
103  if ($this->_entityTypeId === null) {
104  $this->_entityTypeId = (int)$this->_eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)
105  ->getId();
106  }
107  return $this->_entityTypeId;
108  }
109 
115  public function getAttributesUsedInListing()
116  {
117  $connection = $this->getConnection();
118  $storeLabelExpr = $connection->getCheckSql('al.value IS NOT NULL', 'al.value', 'main_table.frontend_label');
119 
120  $select = $connection->select()->from(
121  ['main_table' => $this->getTable('eav_attribute')]
122  )->join(
123  ['additional_table' => $this->getTable('catalog_eav_attribute')],
124  'main_table.attribute_id = additional_table.attribute_id'
125  )->joinLeft(
126  ['al' => $this->getTable('eav_attribute_label')],
127  'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int)$this->getStoreId(),
128  ['store_label' => $storeLabelExpr]
129  )->where(
130  'main_table.entity_type_id = ?',
131  $this->getEntityTypeId()
132  )->where(
133  'additional_table.used_in_product_listing = ?',
134  1
135  );
136  return $connection->fetchAll($select);
137  }
138 
144  public function getAttributesUsedForSortBy()
145  {
146  $connection = $this->getConnection();
147  $storeLabelExpr = $connection->getCheckSql('al.value IS NULL', 'main_table.frontend_label', 'al.value');
148  $select = $connection->select()->from(
149  ['main_table' => $this->getTable('eav_attribute')]
150  )->join(
151  ['additional_table' => $this->getTable('catalog_eav_attribute')],
152  'main_table.attribute_id = additional_table.attribute_id'
153  )->joinLeft(
154  ['al' => $this->getTable('eav_attribute_label')],
155  'al.attribute_id = main_table.attribute_id AND al.store_id = ' . $this->getStoreId(),
156  ['store_label' => $storeLabelExpr]
157  )->where(
158  'main_table.entity_type_id = ?',
159  $this->getEntityTypeId()
160  )->where(
161  'additional_table.used_for_sort_by = ?',
162  1
163  );
164  return $connection->fetchAll($select);
165  }
166 }
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Eav\Model\Config $eavConfig, $connectionName=null)
Definition: Config.php:49
$storeManager
$connection
Definition: bulk.php:13