Magento – How to print sql query for order collection factory in magento 2

collection;databasemagento2query

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);

Best Answer

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

Related Topic