2

I am trying to print sql query. But it is not printing. Please help

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\SessionFactory $customerSession,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    \Magento\Sales\Model\Order\Config $orderConfig,
    array $data = []
){
    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->_customerSession = $customerSession;
    $this->_orderConfig = $orderConfig;
    parent::__construct($context, $data);
}
     public function getOrders()
{
     $customer = $this->_customerSession->create();
     $customerId = $customer->getCustomer()->getId();

    if (!($customerId)) {
        return false;
    }
    if (!$this->orders) {
        $this->orders = $this->_orderCollectionFactory->create($customerId)->addFieldToSelect(
            '*'
        )->addFieldToFilter(
            'status',
            ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
        )->setOrder(
            'created_at',
            'desc'
        )->setPageSize(1)
          ->setCurPage(1)
           ->load();

    }
    $this->_orderCollectionFactory->printLogQuery(true);
1

$this->_orderCollectionFactory is an factory class so you need to create this first.

$this->orders = $this->_orderCollectionFactory->create();
$this->orders->addFieldToSelect(
    '*'
)
->addFieldToFilter('customer_id', $customerId)
->addFieldToFilter(
    'status',
    ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
)->setOrder(
    'created_at',
    'desc'
)->setPageSize(1)
    ->setCurPage(1)
    ->load();
$this->orders->printLogQuery(true);

[Update]

Replace following code:

protected $orders;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\SessionFactory $customerSession,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    \Magento\Sales\Model\Order\Config $orderConfig,
    array $data = []
){
    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->_customerSession = $customerSession;
    $this->_orderConfig = $orderConfig;
    parent::__construct($context, $data);
}

public function getOrders()
{
    $customer = $this->_customerSession->create();
    $customerId = $customer->getCustomer()->getId();

    if (!($customerId)) {
        return false;
    }
    if (!$this->orders) {
        $this->orders = $this->_orderCollectionFactory->create();
        $this->orders->addFieldToSelect(
            '*'
        )
            ->addFieldToFilter('customer_id', $customerId)
            ->addFieldToFilter(
                'status',
                ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
            )->setOrder(
                'created_at',
                'desc'
            )->setPageSize(1)
            ->setCurPage(1)
            ->load();
        $this->orders->printLogQuery(true);
    }

    return $this->orders;
}

Make sure you are login user and clear cache.

  • Hi Sohel, It's not printing sql query. It bypass and load phtml file even I put die(); – Abhishek Mar 15 '17 at 14:38
  • Check updated answer. – Sohel Rana Mar 15 '17 at 15:00
1

Try using getSelect() like

echo $this->_orderCollectionFactory->getSelect();

Hope this will help you.

  • This wont work. orderCollectionFactory has only the method create – Marius Nov 10 '17 at 18:49

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.