답변:
필요에 따라 사용 Magento\Catalog\Model\ProductRepository
하거나 사용해야 Magento\Catalog\Model\ResourceModel\Product\Collection
합니다. 두 가지 방법을 모두 사용하여 모든 데이터가있는 제품 인스턴스를 얻을 수 있습니다.
/**
* @param \Magento\Catalog\Model\ProductRepository $productRepository
* @param \Magento\Framework\Api\SearchCriteriaInterface $criteria
* @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
* @param \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus
* @param \Magento\Catalog\Model\Product\Visibility $productVisibility
*/
public function __construct(
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\Framework\Api\SearchCriteriaInterface $criteria,
\Magento\Framework\Api\Search\FilterGroup $filterGroup,
\Magento\Framework\Api\FilterBuilder $filterBuilder,
\Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
\Magento\Catalog\Model\Product\Visibility $productVisibility
) {
$this->productRepository = $productRepository;
$this->searchCriteria = $criteria;
$this->filterGroup = $filterGroup;
$this->filterBuilder = $filterBuilder;
$this->productStatus = $productStatus;
$this->productVisibility = $productVisibility;
$this->getProductData();
}
/**
* @return \Magento\Cms\Model\Block|null
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
protected function getProductData()
{
$this->filterGroup->setFilters([
$this->filterBuilder
->setField('status')
->setConditionType('in')
->setValue($this->productStatus->getVisibleStatusIds())
->create(),
$this->filterBuilder
->setField('visibility')
->setConditionType('in')
->setValue($this->productVisibility->getVisibleInSiteIds())
->create(),
]);
$this->searchCriteria->setFilterGroups([$this->filterGroup]);
$products = $this->productRepository->getList($this->searchCriteria);
$productItems = $products->getItems();
return $productItems;
}
결과:
/**
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
* @param \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus
* @param \Magento\Catalog\Model\Product\Visibility $productVisibility
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
\Magento\Catalog\Model\Product\Visibility $productVisibility
) {
$this->productCollectionFactory = $productCollectionFactory;
$this->productStatus = $productStatus;
$this->productVisibility = $productVisibility;
}
/**
* @return \Magento\Framework\DataObject[]
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getProducts()
{
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->productCollectionFactory->create();
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
$collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
->addAttributeToFilter('visibility', ['in' => $this->productVisibility->getVisibleInSiteIds()]);
return $collection->getItems();
}
결과:
가장 유력한 답변이 효과가 있지만 여기서는 제품 리포지토리 구현을 직접 주입하여 서비스 계약 원칙을 위반하게되며 이는 마 젠토가 개발자가 수정하기를 원하는 것입니다. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
구현 인 대신 주입해야합니다 \Magento\Catalog\Model\ProductRepository $productRepository
. 이렇게하면 향후 업그레이드 기능을위한 공간이 생깁니다. 결론은 가능한 한 서비스 계약을 사용하는 것입니다.