0

I have below sql query, how do I turn this to Magento get data method to retrieve data? Please help

SELECT sales_order.increment_id, sales_order_payment.additional_information FROM sales_order LEFT JOIN 
 sales_order_payment ON sales_order.entity_id = sales_order_payment.parent_id 
 WHERE sales_order_payment.method = 'opayment' AND sales_order.updated_at >= NOW() - INTERVAL 30 MINUTE
asked Dec 1, 2020 at 20:08

2 Answers 2

1

You can use the below query:

 /**
 * @var $orderCollection Collection
 */
 $orderCollection = $this->collectionFactory->create();
 $orderCollection->getSelect()->joinLeft(
 ['sales_order_payment' => $orderCollection->getTable('sales_order_payment')],
 "(main_table.entity_id = {$orderCollection->getTable('sales_order_payment')}.parent_id" .
 " )",
 [
 $orderCollection->getTable('sales_order_payment') . '.additional_information'
 ]
 )->where($orderCollection->getTable('sales_order_payment').'.method = ?','payflowpro');
 $orderCollection->getSelect()->where('main_table.updated_at >= ?',$orderCollection->getConnection()->getDateSubSql(
 new \Zend_Db_Expr('NOW()'),
 '30',
 \Magento\Framework\DB\Adapter\AdapterInterface::INTERVAL_MINUTE
 ));
answered Dec 3, 2020 at 9:30
2
  • thanks! can you help how to implement OrderCollection to the the code, I am confuse how to set the element up at the page Commented Dec 3, 2020 at 18:57
  • THANKS! YOU ARE MY LIFE SAVER Commented Dec 4, 2020 at 22:50
0

You can use Magento\Framework\App\ResourceConnection to run custom SQL queries

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$query = "SELECT sales_order.increment_id, sales_order_payment.additional_information FROM sales_order LEFT JOIN 
 sales_order_payment ON sales_order.entity_id = sales_order_payment.parent_id 
 WHERE sales_order_payment.method = 'opayment' AND sales_order.updated_at >= NOW() - INTERVAL 30 MINUTE";
$Data = $resource->getConnection()->fetchAll($query);

Via Dependency Injection

public function __construct(
 \Magento\Framework\App\ResourceConnection $resource
 ) {
 $this->_resource = $resource;
 }
public function RunQuery(){
 $query = "SELECT sales_order.increment_id, sales_order_payment.additional_information FROM sales_order LEFT JOIN 
 sales_order_payment ON sales_order.entity_id = sales_order_payment.parent_id 
 WHERE sales_order_payment.method = 'opayment' AND sales_order.updated_at >= NOW() - INTERVAL 30 MINUTE";
 $Data = $this->_resource->getConnection()->fetchAll($query);
}
answered Dec 2, 2020 at 11:19
4
  • If I don't use ObjectManager directly, how do I using Dependency injection? Commented Dec 2, 2020 at 18:03
  • I have updated it in my answer Commented Dec 3, 2020 at 6:11
  • Thanks! Can you tell me why this way is better than using ObjectManager Directly? Commented Dec 3, 2020 at 6:21
  • 1
    read this thread Commented Dec 3, 2020 at 8:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.