I have created my custom module which posts data from magento to an API. How do I fetch the customer, category and order data from the Magento2 database? Will object manager help as a replacement for the Mage::getModel()?
2 Answers
Try this
For Product data select/fetch:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('\Magento\Catalog\Model\Product')->getCollection();
/** Apply filters here */
$productCollection
->addAttributeToSelect('sku')
->addAttributeToSelect('name');
For Category data select/fetch:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryCollection = $objectManager->create('\Magento\Catalog\Model\Category')->getCollection();
/** Apply filters here */
$categoryCollection
->addAttributeToSelect('name');
For Order data select/fetch:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderCollection = $objectManager->create('\Magento\Sales\Model\Order')->getCollection();
/** Apply filters here */
$orderCollection
->addAttributeToSelect('*');
Create resource model instance :-
Contoller file display.php in contoller/forntend/Index/display.php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$postCollection = $objectManager->create('Mageplaza\HelloWorld\Model\Blog')->getCollection();
$postCollection->addFieldToFilter(
array('title', 'content', 'name'),
array(
array('like' => "%$string%"),
array('like' => "%$string%"),
array('like' => "%$string%")
)
);
$postCollection->setOrder('id', 'DESC');
echo $postCollection->getSelect();
$result = $this->resultJsonFactory->create();
$result->setData($postCollection);
using this your select data must be print .... thank u.......