0

Need to join two tables sales_order TO JOIN sales_order_item ON entity_id = order_id

Below is what is existing and would like to join sales_order to this.

Collection.php

<?php namespace Magecar\BackorderItems\Model\ResourceModel\Post; 

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection 
{ 
protected $_idFieldName = 'item_id'; 
protected $_eventPrefix = 'magecar_backorderitems_post_collection'; 
protected $_eventObject = 'post_collection'; 

/** 
* Define resource model 
* 
* @return void 
*/ 
protected function _construct() 
{ 
$this->_init('MageCar\BackorderItems\Model\Post', 'MageCar\BackorderItems\Model\ResourceModel\Post'); 
} }

Post.php

<?php namespace Magecar\BackorderItems\Model\ResourceModel; 
class Post extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb 
{ 
public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context ) 
{ 
parent::__construct($context); 
} 
protected function _construct() 
{ 
$this->_init('sales_order_item', 'item_id'); 
} }

1 Answer 1

0

Please Check below code:-

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$connection = $objectManager->create('\Magento\Framework\App\ResourceConnection');
    $conn = $connection->getConnection();
    $select = $conn->select()
        ->from(
            ['main_table' => 'sales_order'],
            [
                'main_table.customer_firstname',
                'sales_item.price',
                'sales_item.qty_ordered'
            ]
        )
        ->join(
            ['sales_item' => 'sales_order_item'],
            'main_table.entity_id = sales_item.order_id'
        )->order('fieldname','DESC');

$data = $conn->fetchAll($select);
echo "<pre>";
print_r($data); die;

If you have any question let me know.

THANKS.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.