Magento 2 has service contact class for almost most of theall tables(entities).
InSo for this table, you can usemagento has a service contact class
Magento\Sales\Api\OrderItemRepositoryInterface::getList($searchCriteria)
. Using this interface for getting the all data of sales_order_item
table data.
Code:
<?php
namespace MagentostackExchange\Test\Model;
class OrderItems {
/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var \Magento\Sales\Api\OrderItemRepositoryInterface
*/
private $orderItemRepository;
public function __construct(
\Magento\Sales\Api\OrderItemRepositoryInterface $orderItemRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->orderItemRepository = $orderItemRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
public function getALlItemofOrderItem(){
$searchCriteria = $this->searchCriteriaBuilder->create();
$orderItemList = $this->orderItemRepository->getList($searchCriteria);
if($orderItemList->getTotalCount() >= 0)
{
foreach ($orderItemList->getItems() as $orderItem)
{
echo '<pre>';
print_r($orderItem->getData());
}
}
}
}