Magento 2: What is a Resource Model and how can I use it?
-
10-12-2020 - |
سؤال
In Magento 1, using a resource model we are getting collections of data:
$products = Mage::getResourceModel('catalog/product_collection');
$category = Mage::getResourceModel('catalog/category_collection');
$order = Mage::getResourceModel('sales/order_collection');
How can I use it in Magento 2? Is there any special use of it in this version?
المحلول
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;
}
Now,
Using Direct Objectmanager is not proper way to do a coding in 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
نصائح أخرى
Note: As per ECGM2 coding standard you should not use object manager direct in template files
By ObjectManager
//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
By Factory Method
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
}
لا تنتمي إلى magento.stackexchange