4

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?

7

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
| improve this answer | |
  • If i want to call Resourcemodel directly in frontend file without creating my own module then? Is there any way i can call it directly? – jack Aug 22 '17 at 5:21
  • You can directly call using obejctmanager. – Rakesh Jesadiya Aug 22 '17 at 5:23
  • 1
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')->create(); $collection->addAttributeToSelect('*'); – Rakesh Jesadiya Aug 22 '17 at 5:24
  • let me know if you have still query – Rakesh Jesadiya Aug 22 '17 at 5:29
  • 1
    $collection->addFieldToFilter('brand',10) and you can do same as m1 – Rakesh Jesadiya Aug 22 '17 at 9:02
2

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
}
| improve this answer | |
  • Thanks for giving brief information. How do i know about other objectmanager methods? i mean you have written: 'Magento\Catalog\Model\ResourceModel\Product\Collection' How can i check what other methods available? Also, can i filter the collection fetched using object manager ? – jack Aug 22 '17 at 9:02
  • 1
    You can use addAttributeToFilter(), addFieldToFilter(), setPageSize(), and other methods same as Magento 1 – Prince Patel Aug 22 '17 at 9:11
  • 1
    Does nobody use the use statement to keep their code clean? – Tschallacka Aug 22 '17 at 9:14
  • As per the coding standard we can not use objectmanager in magento2 . it means if i need to product collection filtered by specific attribute then i need to create my own module and through factory method i need to do that? – jack Aug 26 '17 at 13:29
  • 1
    Yes off course you should use Factory method instead of objectManager. – Prince Patel Aug 26 '17 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.