1

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 2

2

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('*');
0

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.......

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.