Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
9 
16 abstract class Collection extends \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
17 {
21  const EAV_CODE_PASSWORD_HASH = 'password_hash';
22 
28  protected $_website;
29 
35  protected $_entityType;
36 
40  protected $_storeManager;
41 
53  public function __construct(
54  \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
55  \Psr\Log\LoggerInterface $logger,
56  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
57  \Magento\Framework\Event\ManagerInterface $eventManager,
58  \Magento\Eav\Model\Config $eavConfig,
60  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
61  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
62  ) {
63  $this->_storeManager = $storeManager;
64  parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $eavConfig, $connection, $resource);
65  }
66 
72  abstract protected function _getEntityTypeCode();
73 
82  abstract protected function _getEavWebsiteTable();
83 
90  public function getEntityTypeCode()
91  {
92  return $this->_getEntityTypeCode();
93  }
94 
100  public function getEntityType()
101  {
102  if ($this->_entityType === null) {
103  $this->_entityType = $this->eavConfig->getEntityType($this->_getEntityTypeCode());
104  }
105  return $this->_entityType;
106  }
107 
114  public function setWebsite($website)
115  {
116  $this->_website = $this->_storeManager->getWebsite($website);
117  $this->addBindParam('scope_website_id', $this->_website->getId());
118  return $this;
119  }
120 
126  public function getWebsite()
127  {
128  if ($this->_website === null) {
129  $this->_website = $this->_storeManager->getStore()->getWebsite();
130  }
131  return $this->_website;
132  }
133 
139  protected function _initSelect()
140  {
141  $select = $this->getSelect();
142  $connection = $this->getConnection();
143  $entityType = $this->getEntityType();
144  $extraTable = $entityType->getAdditionalAttributeTable();
145  $mainDescribe = $this->getConnection()->describeTable($this->getResource()->getMainTable());
146  $mainColumns = [];
147 
148  foreach (array_keys($mainDescribe) as $columnName) {
149  $mainColumns[$columnName] = $columnName;
150  }
151 
152  $select->from(['main_table' => $this->getResource()->getMainTable()], $mainColumns);
153 
154  // additional attribute data table
155  $extraDescribe = $connection->describeTable($this->getTable($extraTable));
156  $extraColumns = [];
157  foreach (array_keys($extraDescribe) as $columnName) {
158  if (isset($mainColumns[$columnName])) {
159  continue;
160  }
161  $extraColumns[$columnName] = $columnName;
162  }
163 
164  $this->addBindParam('mt_entity_type_id', (int)$entityType->getId());
165  $select->join(
166  ['additional_table' => $this->getTable($extraTable)],
167  'additional_table.attribute_id = main_table.attribute_id',
168  $extraColumns
169  )->where(
170  'main_table.entity_type_id = :mt_entity_type_id'
171  );
172 
173  // scope values
174 
175  $scopeDescribe = $connection->describeTable($this->_getEavWebsiteTable());
176  unset($scopeDescribe['attribute_id']);
177  $scopeColumns = [];
178  foreach (array_keys($scopeDescribe) as $columnName) {
179  if ($columnName == 'website_id') {
180  $scopeColumns['scope_website_id'] = $columnName;
181  } else {
182  if (isset($mainColumns[$columnName])) {
183  $alias = 'scope_' . $columnName;
184  $condition = 'main_table.' . $columnName . ' IS NULL';
185  $true = 'scope_table.' . $columnName;
186  $false = 'main_table.' . $columnName;
187  $expression = $connection->getCheckSql($condition, $true, $false);
188  $this->addFilterToMap($columnName, $expression);
189  $scopeColumns[$alias] = $columnName;
190  } elseif (isset($extraColumns[$columnName])) {
191  $alias = 'scope_' . $columnName;
192  $condition = 'additional_table.' . $columnName . ' IS NULL';
193  $true = 'scope_table.' . $columnName;
194  $false = 'additional_table.' . $columnName;
195  $expression = $connection->getCheckSql($condition, $true, $false);
196  $this->addFilterToMap($columnName, $expression);
197  $scopeColumns[$alias] = $columnName;
198  }
199  }
200  }
201 
202  $select->joinLeft(
203  ['scope_table' => $this->_getEavWebsiteTable()],
204  'scope_table.attribute_id = main_table.attribute_id AND scope_table.website_id = :scope_website_id',
205  $scopeColumns
206  );
207  $websiteId = $this->getWebsite() ? (int)$this->getWebsite()->getId() : 0;
208  $this->addBindParam('scope_website_id', $websiteId);
209 
210  return $this;
211  }
212 
222  public function setEntityTypeFilter($type)
223  {
224  return $this;
225  }
226 
233  public function addVisibleFilter()
234  {
235  return $this->addFieldToFilter('is_visible', 1);
236  }
237 
243  public function addSystemHiddenFilter()
244  {
245  $connection = $this->getConnection();
246  $expression = $connection->getCheckSql(
247  'additional_table.is_system = 1 AND additional_table.is_visible = 0',
248  '1',
249  '0'
250  );
251  $this->getSelect()->where($connection->quoteInto($expression . ' = ?', 0));
252  return $this;
253  }
254 
261  {
262  $connection = $this->getConnection();
263  $expression = $connection->getCheckSql(
264  $connection->quoteInto(
265  'additional_table.is_system = 1 AND additional_table.is_visible = 0 AND main_table.attribute_code != ?',
266  self::EAV_CODE_PASSWORD_HASH
267  ),
268  '1',
269  '0'
270  );
271  $this->getSelect()->where($connection->quoteInto($expression . ' = ?', 0));
272  return $this;
273  }
274 
282  {
283  return $this->addFieldToFilter('main_table.frontend_input', ['neq' => 'hidden']);
284  }
285 }
addFilterToMap($filter, $alias, $group='fields')
Definition: AbstractDb.php:798
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$storeManager
$resource
Definition: bulk.php:12
$logger
$type
Definition: item.phtml:13
__construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Eav\Model\Config $eavConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
Definition: Collection.php:53
if(!trim($html)) $alias
Definition: details.phtml:20
$connection
Definition: bulk.php:13