Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Collection Class Reference
Inheritance diagram for Collection:
AbstractCollection AbstractCollection AbstractDb SourceProviderInterface Collection ArrayInterface CollectionDataSourceInterface OptionSourceInterface ArgumentInterface

Public Member Functions

 __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\Framework\App\ResourceConnection $resource, \Magento\Eav\Model\EntityFactory $eavEntityFactory, \Magento\Eav\Model\ResourceModel\Helper $resourceHelper, \Magento\Framework\Validator\UniversalFactory $universalFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig=null)
 
 addIdFilter ($categoryIds)
 
 setLoadProductCount ($flag)
 
 setProductStoreId ($storeId)
 
 getProductStoreId ()
 
 load ($printQuery=false, $logQuery=false)
 
 loadProductCount ($items, $countRegular=true, $countAnchor=true)
 
 addPathFilter ($regexp)
 
 joinUrlRewrite ()
 
 addIsActiveFilter ()
 
 addNameToResult ()
 
 addUrlRewriteToResult ()
 
 addPathsFilter ($paths)
 
 addLevelFilter ($level)
 
 addRootLevelFilter ()
 
 addNavigationMaxDepthFilter ()
 
 addOrderField ($field)
 
 getProductWebsiteTable ()
 
 getProductTable ()
 
- Public Member Functions inherited from AbstractCollection
 __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\Framework\App\ResourceConnection $resource, \Magento\Eav\Model\EntityFactory $eavEntityFactory, \Magento\Eav\Model\ResourceModel\Helper $resourceHelper, \Magento\Framework\Validator\UniversalFactory $universalFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null)
 
 setStore ($store)
 
 setStoreId ($storeId)
 
 getStoreId ()
 
 getDefaultStoreId ()
 
- Public Member Functions inherited from AbstractCollection
 __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\Framework\App\ResourceConnection $resource, \Magento\Eav\Model\EntityFactory $eavEntityFactory, \Magento\Eav\Model\ResourceModel\Helper $resourceHelper, \Magento\Framework\Validator\UniversalFactory $universalFactory, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null)
 
 getTable ($table)
 
 setEntity ($entity)
 
 getEntity ()
 
 getResource ()
 
 setObject ($object=null)
 
 addItem (\Magento\Framework\DataObject $object)
 
 getAttribute ($attributeCode)
 
 addAttributeToFilter ($attribute, $condition=null, $joinType='inner')
 
 addFieldToFilter ($attribute, $condition=null)
 
 addAttributeToSort ($attribute, $dir=self::SORT_ORDER_ASC)
 
 addAttributeToSelect ($attribute, $joinType=false)
 
 addEntityTypeToSelect ($entityType, $prefix)
 
 addStaticField ($field)
 
 addExpressionAttributeToSelect ($alias, $expression, $attribute)
 
 groupByAttribute ($attribute)
 
 joinAttribute ($alias, $attribute, $bind, $filter=null, $joinType='inner', $storeId=null)
 
 joinField ($alias, $table, $field, $bind, $cond=null, $joinType='inner')
 
 joinTable ($table, $bind, $fields=null, $cond=null, $joinType='inner')
 
 removeAttributeToSelect ($attribute=null)
 
 setPage ($pageNum, $pageSize)
 
 load ($printQuery=false, $logQuery=false)
 
 getAllIds ($limit=null, $offset=null)
 
 getAllIdsSql ()
 
 save ()
 
 delete ()
 
 importFromArray ($arr)
 
 exportToArray ()
 
 getRowIdFieldName ()
 
 getIdFieldName ()
 
 setRowIdFieldName ($fieldName)
 
 _loadAttributes ($printQuery=false, $logQuery=false)
 
 setOrder ($attribute, $dir=self::SORT_ORDER_ASC)
 
 toArray ($arrAttributes=[])
 
 isAttributeAdded ($attributeCode)
 
 getLoadedIds ()
 
 clear ()
 
 removeAllItems ()
 
 removeItemByKey ($key)
 
 getMainTable ()
 
 addFieldToSelect ($field, $alias=null)
 
 removeFieldFromSelect ($field)
 
 removeAllFieldsFromSelect ()
 
- Public Member Functions inherited from AbstractDb
 __construct (EntityFactoryInterface $entityFactory, Logger $logger, FetchStrategyInterface $fetchStrategy, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null)
 
 getResource ()
 
 addBindParam ($name, $value)
 
 getIdFieldName ()
 
 setConnection (\Magento\Framework\DB\Adapter\AdapterInterface $conn)
 
 getSelect ()
 
 getConnection ()
 
 getSize ()
 
 getSelectCountSql ()
 
 getSelectSql ($stringMode=false)
 
 setOrder ($field, $direction=self::SORT_ORDER_DESC)
 
 addOrder ($field, $direction=self::SORT_ORDER_DESC)
 
 unshiftOrder ($field, $direction=self::SORT_ORDER_DESC)
 
 addFieldToFilter ($field, $condition=null)
 
 distinct ($flag)
 
 load ($printQuery=false, $logQuery=false)
 
 loadWithFilter ($printQuery=false, $logQuery=false)
 
 fetchItem ()
 
 getData ()
 
 resetData ()
 
 loadData ($printQuery=false, $logQuery=false)
 
 printLogQuery ($printQuery=false, $logQuery=false, $sql=null)
 
 addFilterToMap ($filter, $alias, $group='fields')
 
 __clone ()
 
 joinExtensionAttribute (JoinDataInterface $join, JoinProcessorInterface $extensionAttributesJoinProcessor)
 
 getItemObjectClass ()
 
 __sleep ()
 
 __wakeup ()
 
- Public Member Functions inherited from Collection
 __construct (EntityFactoryInterface $entityFactory)
 
 addFilter ($field, $value, $type='and')
 
 addFieldToFilter ($field, $condition)
 
 getFilter ($field)
 
 isLoaded ()
 
 getCurPage ($displacement=0)
 
 getLastPageNumber ()
 
 getPageSize ()
 
 getSize ()
 
 getFirstItem ()
 
 getLastItem ()
 
 getItems ()
 
 getColumnValues ($colName)
 
 getItemsByColumnValue ($column, $value)
 
 getItemByColumnValue ($column, $value)
 
 addItem (\Magento\Framework\DataObject $item)
 
 getAllIds ()
 
 removeItemByKey ($key)
 
 removeAllItems ()
 
 clear ()
 
 walk ($callback, array $args=[])
 
 each ($objMethod, $args=[])
 
 setDataToAll ($key, $value=null)
 
 setCurPage ($page)
 
 setPageSize ($size)
 
 setOrder ($field, $direction=self::SORT_ORDER_DESC)
 
 setItemObjectClass ($className)
 
 getNewEmptyItem ()
 
 distinct ($flag)
 
 loadData ($printQuery=false, $logQuery=false)
 
 load ($printQuery=false, $logQuery=false)
 
 loadWithFilter ($printQuery=false, $logQuery=false)
 
 toXml ()
 
 toArray ($arrRequiredFields=[])
 
 toOptionArray ()
 
 toOptionHash ()
 
 getItemById ($idValue)
 
 getIterator ()
 
 count ()
 
 getFlag ($flag)
 
 setFlag ($flag, $value=null)
 
 hasFlag ($flag)
 
 __sleep ()
 
 __wakeup ()
 
- Public Member Functions inherited from SourceProviderInterface
 getSelect ()
 

Protected Member Functions

 _construct ()
 
 _beforeLoad ()
 
 _afterLoad ()
 
 _loadProductCount ()
 
- Protected Member Functions inherited from AbstractCollection
 getEntityPkName (\Magento\Eav\Model\Entity\AbstractEntity $entity)
 
 _getLoadAttributesSelect ($table, $attributeIds=[])
 
 _addLoadAttributesSelectValues ($select, $table, $type)
 
 _joinAttributeToSelect ($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias)
 
- Protected Member Functions inherited from AbstractCollection
 _construct ()
 
 _prepareStaticFields ()
 
 _initSelect ()
 
 _init ($model, $entityModel)
 
 _prepareOrderExpression ($field)
 
 _getAllIdsSelect ($limit=null, $offset=null)
 
 _getLoadAttributesSelect ($table, $attributeIds=[])
 
 _addLoadAttributesSelectValues ($select, $table, $type)
 
 _setItemAttributeValue ($valueInfo)
 
 _getAttributeTableAlias ($attributeCode)
 
 _getAttributeFieldName ($attributeCode)
 
 _addAttributeJoin ($attributeCode, $joinType='inner')
 
 getEntityPkName (\Magento\Eav\Model\Entity\AbstractEntity $entity)
 
 _joinAttributeToSelect ($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias)
 
 _getAttributeConditionSql ($attribute, $condition, $joinType='inner')
 
 _renderOrders ()
 
 _afterLoad ()
 
 _reset ()
 
- Protected Member Functions inherited from AbstractDb
 _setIdFieldName ($fieldName)
 
 _getItemId (\Magento\Framework\DataObject $item)
 
 _renderFilters ()
 
 _renderFiltersBefore ()
 
 _translateCondition ($field, $condition)
 
 _getMappedField ($field)
 
 _getMapper ()
 
 _getConditionSql ($fieldName, $condition)
 
 _getConditionFieldName ($fieldName)
 
 _renderOrders ()
 
 _renderLimit ()
 
 _beforeLoad ()
 
 beforeAddLoadedItem (\Magento\Framework\DataObject $item)
 
 _toOptionArray ($valueField=null, $labelField='name', $additional=[])
 
 _toOptionHash ($valueField=null, $labelField='name')
 
 _afterLoadData ()
 
 _afterLoad ()
 
 _logQuery ($sql)
 
 _reset ()
 
 _fetchAll (Select $select)
 
 _initSelect ()
 
- Protected Member Functions inherited from Collection
 _setIsLoaded ($flag=true)
 
 _addItem ($item)
 
 _getItemId (\Magento\Framework\DataObject $item)
 
 _renderFilters ()
 
 _renderOrders ()
 
 _renderLimit ()
 
 _toOptionArray ($valueField='id', $labelField='name', $additional=[])
 
 _toOptionHash ($valueField='id', $labelField='name')
 

Protected Attributes

 $_eventPrefix = 'catalog_category_collection'
 
 $_eventObject = 'category_collection'
 
 $_productStoreId
 
 $_loadWithProductCount = false
 
- Protected Attributes inherited from AbstractCollection
 $_storeId
 
 $_storeManager
 
- Protected Attributes inherited from AbstractCollection
 $_itemsById = []
 
 $_staticFields = []
 
 $_entity
 
 $_selectEntityTypes = []
 
 $_selectAttributes = []
 
 $_filterAttributes = []
 
 $_joinEntities = []
 
 $_joinAttributes = []
 
 $_joinFields = []
 
 $_castToIntMap = ['validate-digits']
 
 $_eventManager = null
 
 $_eavConfig
 
 $_resource
 
 $_eavEntityFactory
 
 $_resourceHelper
 
 $_universalFactory
 
- Protected Attributes inherited from AbstractDb
 $_conn
 
 $_select
 
 $_idFieldName
 
 $_bindParams = []
 
 $_data = null
 
 $_map = null
 
 $_fetchStmt = null
 
 $_isOrdersRendered = false
 
 $_logger
 
 $extensionAttributesJoinProcessor
 
- Protected Attributes inherited from Collection
 $_items = []
 
 $_itemObjectClass = \Magento\Framework\DataObject::class
 
 $_orders = []
 
 $_filters = []
 
 $_isFiltersRendered = false
 
 $_curPage = 1
 
 $_pageSize = false
 
 $_totalRecords
 
 $_isCollectionLoaded
 
 $_flags = []
 
 $_entityFactory
 

Additional Inherited Members

- Data Fields inherited from AbstractCollection
const ATTRIBUTE_TABLE_ALIAS_PREFIX = 'at_'
 
- Data Fields inherited from Collection
const SORT_ORDER_ASC = 'ASC'
 
const SORT_ORDER_DESC = 'DESC'
 

Detailed Description

Category resource collection

@api

Author
Magento Core Team core@.nosp@m.mage.nosp@m.ntoco.nosp@m.mmer.nosp@m.ce.co.nosp@m.m @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Since
100.0.2

Definition at line 20 of file Collection.php.

Constructor & Destructor Documentation

◆ __construct()

__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\Framework\App\ResourceConnection  $resource,
\Magento\Eav\Model\EntityFactory  $eavEntityFactory,
\Magento\Eav\Model\ResourceModel\Helper  $resourceHelper,
\Magento\Framework\Validator\UniversalFactory  $universalFactory,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Framework\DB\Adapter\AdapterInterface  $connection = null,
\Magento\Framework\App\Config\ScopeConfigInterface  $scopeConfig = null 
)

Constructor

Parameters
\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\Framework\App\ResourceConnection$resource
\Magento\Eav\Model\EntityFactory$eavEntityFactory
\Magento\Eav\Model\ResourceModel\Helper$resourceHelper
\Magento\Framework\Validator\UniversalFactory$universalFactory
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Framework\DB\Adapter\AdapterInterface$connection
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 88 of file Collection.php.

101  {
102  parent::__construct(
103  $entityFactory,
104  $logger,
105  $fetchStrategy,
106  $eventManager,
107  $eavConfig,
108  $resource,
109  $eavEntityFactory,
110  $resourceHelper,
111  $universalFactory,
114  );
115  $this->scopeConfig = $scopeConfig ?:
116  \Magento\Framework\App\ObjectManager::getInstance()->get(ScopeConfigInterface::class);
117  }
$storeManager
$resource
Definition: bulk.php:12
$logger
$connection
Definition: bulk.php:13

Member Function Documentation

◆ _afterLoad()

_afterLoad ( )
protected

After collection load

Returns
$this

Definition at line 185 of file Collection.php.

186  {
187  $this->_eventManager->dispatch($this->_eventPrefix . '_load_after', [$this->_eventObject => $this]);
188 
189  return parent::_afterLoad();
190  }

◆ _beforeLoad()

_beforeLoad ( )
protected

Before collection load

Returns
$this

Definition at line 174 of file Collection.php.

175  {
176  $this->_eventManager->dispatch($this->_eventPrefix . '_load_before', [$this->_eventObject => $this]);
177  return parent::_beforeLoad();
178  }

◆ _construct()

_construct ( )
protected

Init collection and determine table names

Returns
void

Definition at line 124 of file Collection.php.

125  {
126  $this->_init(\Magento\Catalog\Model\Category::class, \Magento\Catalog\Model\ResourceModel\Category::class);
127  }

◆ _loadProductCount()

_loadProductCount ( )
protected

Load categories product count

Returns
void

Definition at line 249 of file Collection.php.

250  {
251  $this->loadProductCount($this->_items, true, true);
252  }
loadProductCount($items, $countRegular=true, $countAnchor=true)
Definition: Collection.php:264

◆ addIdFilter()

addIdFilter (   $categoryIds)

Add Id filter

Parameters
array$categoryIds
Returns
$this

Definition at line 135 of file Collection.php.

136  {
137  if (is_array($categoryIds)) {
138  if (empty($categoryIds)) {
139  $condition = '';
140  } else {
141  $condition = ['in' => $categoryIds];
142  }
143  } elseif (is_numeric($categoryIds)) {
144  $condition = $categoryIds;
145  } elseif (is_string($categoryIds)) {
146  $ids = explode(',', $categoryIds);
147  if (empty($ids)) {
148  $condition = $categoryIds;
149  } else {
150  $condition = ['in' => $ids];
151  }
152  }
153  $this->addFieldToFilter('entity_id', $condition);
154  return $this;
155  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ addIsActiveFilter()

addIsActiveFilter ( )

Add active category filter

Returns
$this

Definition at line 387 of file Collection.php.

388  {
389  $this->addAttributeToFilter('is_active', 1);
390  $this->_eventManager->dispatch(
391  $this->_eventPrefix . '_add_is_active_filter',
392  [$this->_eventObject => $this]
393  );
394  return $this;
395  }
addAttributeToFilter($attribute, $condition=null, $joinType='inner')

◆ addLevelFilter()

addLevelFilter (   $level)

Add category level filter

Parameters
int | string$level
Returns
$this

Definition at line 447 of file Collection.php.

448  {
449  $this->addFieldToFilter('level', ['lteq' => $level]);
450  return $this;
451  }

◆ addNameToResult()

addNameToResult ( )

Add name attribute to result

Returns
$this

Definition at line 402 of file Collection.php.

403  {
404  $this->addAttributeToSelect('name');
405  return $this;
406  }

◆ addNavigationMaxDepthFilter()

addNavigationMaxDepthFilter ( )

Add navigation max depth filter

Returns
$this

Definition at line 470 of file Collection.php.

471  {
472  $navigationMaxDepth = (int)$this->scopeConfig->getValue(
473  'catalog/navigation/max_depth',
474  ScopeInterface::SCOPE_STORE
475  );
476  if ($navigationMaxDepth > 0) {
477  $this->addLevelFilter($navigationMaxDepth);
478  }
479  return $this;
480  }

◆ addOrderField()

addOrderField (   $field)

Add order field

Parameters
string$field
Returns
$this

Definition at line 488 of file Collection.php.

489  {
490  $this->setOrder($field, self::SORT_ORDER_ASC);
491  return $this;
492  }

◆ addPathFilter()

addPathFilter (   $regexp)

Add category path filter

Parameters
string$regexp
Returns
$this

Definition at line 355 of file Collection.php.

356  {
357  $this->addFieldToFilter('path', ['regexp' => $regexp]);
358  return $this;
359  }

◆ addPathsFilter()

addPathsFilter (   $paths)

Add category path filter

Parameters
array | string$paths
Returns
$this

Definition at line 425 of file Collection.php.

426  {
427  if (!is_array($paths)) {
428  $paths = [$paths];
429  }
430  $connection = $this->getResource()->getConnection();
431  $cond = [];
432  foreach ($paths as $path) {
433  $cond[] = $connection->quoteInto('e.path LIKE ?', "{$path}%");
434  }
435  if ($cond) {
436  $this->getSelect()->where(join(' OR ', $cond));
437  }
438  return $this;
439  }
$paths
Definition: _bootstrap.php:83
$connection
Definition: bulk.php:13

◆ addRootLevelFilter()

addRootLevelFilter ( )

Add root category filter

Returns
$this

Definition at line 458 of file Collection.php.

459  {
460  $this->addFieldToFilter('path', ['neq' => '1']);
461  $this->addLevelFilter(1);
462  return $this;
463  }

◆ addUrlRewriteToResult()

addUrlRewriteToResult ( )

Add url rewrite rules to collection

Returns
$this

Definition at line 413 of file Collection.php.

414  {
415  $this->joinUrlRewrite();
416  return $this;
417  }

◆ getProductStoreId()

getProductStoreId ( )

Get id of the store that we should count products on

Returns
int

Definition at line 209 of file Collection.php.

210  {
211  if ($this->_productStoreId === null) {
212  $this->_productStoreId = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
213  }
214  return $this->_productStoreId;
215  }

◆ getProductTable()

getProductTable ( )

Getter for _productTable

Returns
string

Definition at line 512 of file Collection.php.

513  {
514  if (empty($this->_productTable)) {
515  $this->_productTable = $this->getTable('catalog_category_product');
516  }
517  return $this->_productTable;
518  }

◆ getProductWebsiteTable()

getProductWebsiteTable ( )

Getter for _productWebsiteTable

Returns
string

Definition at line 499 of file Collection.php.

500  {
501  if (empty($this->_productWebsiteTable)) {
502  $this->_productWebsiteTable = $this->getTable('catalog_product_website');
503  }
504  return $this->_productWebsiteTable;
505  }

◆ joinUrlRewrite()

joinUrlRewrite ( )

Joins url rewrite rules to collection

Returns
$this

Definition at line 366 of file Collection.php.

367  {
368  $this->joinTable(
369  'url_rewrite',
370  'entity_id = entity_id',
371  ['request_path'],
372  sprintf(
373  '{{table}}.is_autogenerated = 1 AND {{table}}.store_id = %d AND {{table}}.entity_type = \'%s\'',
374  $this->getStoreId(),
376  ),
377  'left'
378  );
379  return $this;
380  }
joinTable($table, $bind, $fields=null, $cond=null, $joinType='inner')

◆ load()

load (   $printQuery = false,
  $logQuery = false 
)

Load collection

Parameters
bool$printQuery
bool$logQuery
Returns
$this

Definition at line 224 of file Collection.php.

225  {
226  if ($this->isLoaded()) {
227  return $this;
228  }
229 
230  if ($this->_loadWithProductCount) {
231  $this->addAttributeToSelect('all_children');
232  $this->addAttributeToSelect('is_anchor');
233  }
234 
235  parent::load($printQuery, $logQuery);
236 
237  if ($this->_loadWithProductCount) {
238  $this->_loadProductCount();
239  }
240 
241  return $this;
242  }

◆ loadProductCount()

loadProductCount (   $items,
  $countRegular = true,
  $countAnchor = true 
)

Load product count for specified items

Parameters
array$items
boolean$countRegularget product count for regular (non-anchor) categories
boolean$countAnchorget product count for anchor categories
Returns
$this @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.UnusedLocalVariable)

Definition at line 264 of file Collection.php.

265  {
266  $anchor = [];
267  $regular = [];
268  $websiteId = $this->_storeManager->getStore($this->getProductStoreId())->getWebsiteId();
269 
270  foreach ($items as $item) {
271  if ($item->getIsAnchor()) {
272  $anchor[$item->getId()] = $item;
273  } else {
274  $regular[$item->getId()] = $item;
275  }
276  }
277 
278  if ($countRegular) {
279  // Retrieve regular categories product counts
280  $regularIds = array_keys($regular);
281  if (!empty($regularIds)) {
282  $select = $this->_conn->select();
283  $select->from(
284  ['main_table' => $this->getProductTable()],
285  ['category_id', new \Zend_Db_Expr('COUNT(main_table.product_id)')]
286  )->where(
287  $this->_conn->quoteInto('main_table.category_id IN(?)', $regularIds)
288  )->group(
289  'main_table.category_id'
290  );
291  if ($websiteId) {
292  $select->join(
293  ['w' => $this->getProductWebsiteTable()],
294  'main_table.product_id = w.product_id',
295  []
296  )->where(
297  'w.website_id = ?',
298  $websiteId
299  );
300  }
301  $counts = $this->_conn->fetchPairs($select);
302  foreach ($regular as $item) {
303  if (isset($counts[$item->getId()])) {
304  $item->setProductCount($counts[$item->getId()]);
305  } else {
306  $item->setProductCount(0);
307  }
308  }
309  }
310  }
311 
312  if ($countAnchor) {
313  // Retrieve Anchor categories product counts
314  foreach ($anchor as $item) {
315  if ($allChildren = $item->getAllChildren()) {
316  $bind = ['entity_id' => $item->getId(), 'c_path' => $item->getPath() . '/%'];
317  $select = $this->_conn->select();
318  $select->from(
319  ['main_table' => $this->getProductTable()],
320  new \Zend_Db_Expr('COUNT(DISTINCT main_table.product_id)')
321  )->joinInner(
322  ['e' => $this->getTable('catalog_category_entity')],
323  'main_table.category_id=e.entity_id',
324  []
325  )->where(
326  'e.entity_id = :entity_id'
327  )->orWhere(
328  'e.path LIKE :c_path'
329  );
330  if ($websiteId) {
331  $select->join(
332  ['w' => $this->getProductWebsiteTable()],
333  'main_table.product_id = w.product_id',
334  []
335  )->where(
336  'w.website_id = ?',
337  $websiteId
338  );
339  }
340  $item->setProductCount((int)$this->_conn->fetchOne($select, $bind));
341  } else {
342  $item->setProductCount(0);
343  }
344  }
345  }
346  return $this;
347  }
$items

◆ setLoadProductCount()

setLoadProductCount (   $flag)

Set flag for loading product count

Parameters
boolean$flag
Returns
$this

Definition at line 163 of file Collection.php.

164  {
165  $this->_loadWithProductCount = $flag;
166  return $this;
167  }

◆ setProductStoreId()

setProductStoreId (   $storeId)

Set id of the store that we should count products on

Parameters
int$storeId
Returns
$this

Definition at line 198 of file Collection.php.

199  {
200  $this->_productStoreId = $storeId;
201  return $this;
202  }

Field Documentation

◆ $_eventObject

$_eventObject = 'category_collection'
protected

Definition at line 34 of file Collection.php.

◆ $_eventPrefix

$_eventPrefix = 'catalog_category_collection'
protected

Definition at line 27 of file Collection.php.

◆ $_loadWithProductCount

$_loadWithProductCount = false
protected

Definition at line 62 of file Collection.php.

◆ $_productStoreId

$_productStoreId
protected

Definition at line 48 of file Collection.php.


The documentation for this class was generated from the following file: