I have below code
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderRepository = $objectManager->get('\Magento\Sales\Api\OrderRepositoryInterface');
$customModel = $objectManager->get('\Custom\Module\Model\ResourceModel\Index\Collection');
$orderIdsArray = array(3123,3124);
if(count($orderIdsArray) > 0 ){
foreach($orderIdsArray as $orderId){
$oId = $orderRepository->get($orderId);
if($oId->getId()){
$items = $oId->getAllItems();
if (count($items) > 0 && count($items) == 1) {
foreach ($items as $item) {
$products = $customModel->addFieldToFilter('product_id', $item->getProductId());
echo $products->getSelect()->__toString().'='.count($products);
}
}
}
}
}
First query it print
SELECT `main_table`.* FROM `custom_table` AS `main_table` WHERE (`product_id` = '2482')
SELECT `main_table`.* FROM `custom_table` AS `main_table` WHERE (`product_id` = '2482') AND (`product_id` = '1') <-- WRONG
How can i get below ones?
SELECT `main_table`.* FROM `custom_table` AS `main_table` WHERE (`product_id` = '1') <-- CORRECT