Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductCategoryList.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Model;
7 
13 
18 {
22  private $categoryIdList = [];
23 
27  private $productResource;
28 
32  private $category;
33 
37  private $storeManager;
38 
42  private $tableMaintainer;
43 
50  public function __construct(
51  ResourceModel\Product $productResource,
52  ResourceModel\Category $category,
53  StoreManagerInterface $storeManager = null,
54  TableMaintainer $tableMaintainer = null
55  ) {
56  $this->productResource = $productResource;
57  $this->category = $category;
58  $this->tableMaintainer = $tableMaintainer ?: ObjectManager::getInstance()->get(TableMaintainer::class);
59  $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
60  }
61 
68  public function getCategoryIds($productId)
69  {
70  if (!isset($this->categoryIdList[$productId])) {
71  $unionTables[] = $this->getCategorySelect($productId, $this->category->getCategoryProductTable());
72  foreach ($this->storeManager->getStores() as $store) {
73  $unionTables[] = $this->getCategorySelect(
74  $productId,
75  $this->tableMaintainer->getMainTable($store->getId())
76  );
77  }
78  $unionSelect = new UnionExpression(
79  $unionTables,
81  );
82 
83  $this->categoryIdList[$productId] = $this->productResource->getConnection()->fetchCol($unionSelect);
84  }
85 
86  return $this->categoryIdList[$productId];
87  }
88 
97  {
98  return $this->productResource->getConnection()->select()->from(
99  $tableName,
100  ['category_id']
101  )->where(
102  'product_id = ?',
103  $productId
104  );
105  }
106 }
$tableName
Definition: trigger.php:13
const SQL_UNION_ALL
Definition: Select.php:69
__construct(ResourceModel\Product $productResource, ResourceModel\Category $category, StoreManagerInterface $storeManager=null, TableMaintainer $tableMaintainer=null)