Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ChildrenCategoriesProvider.php
Go to the documentation of this file.
1 <?php
7 
9 
11 {
15  protected $childrenIds = [];
16 
22  public function getChildren(Category $category, $recursive = false)
23  {
24  return $category->isObjectNew() ? [] : $category->getResourceCollection()
25  ->addAttributeToSelect('url_path')
26  ->addAttributeToSelect('url_key')
27  ->addAttributeToSelect('name')
28  ->addIdFilter($this->getChildrenIds($category, $recursive));
29  }
30 
36  public function getChildrenIds(Category $category, $recursive = false)
37  {
38  $cacheKey = $category->getId() . '_' . (int)$recursive;
39  if (!isset($this->childrenIds[$cacheKey])) {
40  $connection = $category->getResource()->getConnection();
41  $select = $connection->select()
42  ->from($category->getResource()->getEntityTable(), 'entity_id')
43  ->where($connection->quoteIdentifier('path') . ' LIKE :c_path');
44  $bind = ['c_path' => $category->getPath() . '/%'];
45  if (!$recursive) {
46  $select->where($connection->quoteIdentifier('level') . ' <= :c_level');
47  $bind['c_level'] = $category->getLevel() + 1;
48  }
49  $this->childrenIds[$cacheKey] = $connection->fetchCol($select, $bind);
50  }
51  return $this->childrenIds[$cacheKey];
52  }
53 }
$connection
Definition: bulk.php:13