Requested entity doesn\'t exist in Magento 2
I had the same error showing on Admin > Sales > Orders
when trying to access a list of all orders.
It turned out to be a discrepancy between two tables sales_order
and sales_order_grid
. Some records in sales_order_grid
didn't have corresponding records in sales_order
table. Luckily, it was my dev environment so I could easily remove offending records. So if you get this error (Requested entity doesn't exist) it's most likely a data integrity issue.
\Magento\Sales\Api\OrderRepositoryInterface::get
receive order id
, not increment_id
. Use getList
, if you need to search order by increment_id
/**
* @param OrderRepositoryInterface $orderRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
/**
* Get order by increment id.
*
* @param string $incrementId
* @return OrderInterface
* @throws NoSuchEntityException
*/
public function getOrderByIncrementId($incrementId)
{
$searchCriteria = $this->searchCriteriaBuilder->addFilter(
OrderInterface::INCREMENT_ID,
$incrementId
)->create();
$result = $this->orderRepository->getList($searchCriteria);
if (empty($result->getItems())) {
throw new NoSuchEntityException(__('No such order.'));
}
$orders = $result->getItems();
return reset($orders);
}