Magento 2: Что такое модель ресурсов и как я могу ее использовать?


В Magento 1, используя модель ресурсов, мы получаем наборы данных:

$products = Mage::getResourceModel('catalog/product_collection');

$category = Mage::getResourceModel('catalog/category_collection');

$order    = Mage::getResourceModel('sales/order_collection');

Как я могу использовать его в Magento 2? Есть ли какое-то особое его использование в этой версии?

Author: Glorfindel, 2017-08-22

2 answers

You can get product,category and order collection by Proper way,

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,      
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoriesCollection,  
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    array $data = []
)
{    
    $this->_productCollectionFactory = $productCollectionFactory;   
    $this->_categoriesCollection = $categoriesCollection;     
    $this->orderCollectionFactory = $orderCollectionFactory;
    parent::__construct($context, $data);
}
/*
* Product collection Data
*/
public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    return $collection;
}
/*
* Category collection Data
*/
public function getCategoryCollection()
{
    $collection = $this->_categoriesCollection->create();
    $collection->addAttributeToSelect('*');
    return $collection;
}
/*
* Order collection Data
*/
public function getOrderCollection()
{
    $order = $orders = $this->orderCollectionFactory->create();    
    $order->addAttributeToSort('created_at', 'desc');
    return $order;
}

Теперь,

Использование Direct Objectmanager не является правильным способом кодирования в magento 2.

Using objectmanager in template file,

//get product collection using objectmanager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productCollection->load();
echo "<pre>";print_r($productCollection->getData()); //print product collection

//get category collection using objectmanager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryCollection = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Category\Collection');
$categoryCollection->load();
echo "<pre>";print_r($categoryCollection->getData()); //print category collection

//get order collection using objectmanager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderCollection = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\Collection');
$orderCollection->load();
echo "<pre>";print_r($orderCollection->getData()); //print category collection
 7
Author: Rakesh Jesadiya, 2017-08-22 09:06:24

Примечание: В соответствии со стандартом кодирования ECGM2 вы не должны использовать диспетчер объектов напрямую в файлах шаблонов

С помощью диспетчера объектов

//get product collection
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productCollection->load();
print_r($productCollection->getData()); //print product collection

//get category collection
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryCollection = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categoryCollection->load();
print_r($categoryCollection->getData()); //print category collection

//get order collection
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderCollection = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
$orderCollection->load();
print_r($orderCollection->getData()); //print category collection

Заводским Методом

public function __construct(
    ...     
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,      
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoriesCollection,  
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    ...
) {
    ...
    $this->_productCollectionFactory = $productCollectionFactory;   
    $this->_categoriesCollection = $categoriesCollection;     
    $this->orderCollectionFactory = $orderCollectionFactory;
    ...
}

public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    return $collection; // return product collection
}

public function getCategoryCollection()
{
    $collection = $this->_categoriesCollection->create();
    $collection->addAttributeToSelect('*');
    return $collection; //return category collection
}

public function getOrderCollection()
{
    $order = $orders = $this->orderCollectionFactory->create();
    return $order; //return order collection
}
 2
Author: Prince Patel, 2017-08-22 09:15:28