I want to get the First item from the collection so I have used following code. But it gives some warning on the usage of getFirstItem()
protected $_sampleFactory;
public function __construct(
\Vendor\Module\Model\sampleFactory $sampleFactory,
) {
$this->_sampleFactory = $sampleFactory;
}
public function execute()
{
$collection = $this->_sampleFactory->create()->getCollection()->addFieldToFilter("product_id", $product_id);
$customer_id = $collection->getFirstItem()->getCustomerId();
return $customer_id;
}
This code is working properly but when I checked with magento-coding-standard tool its given me the warning on usage of getFirstItem()
as follow.
107 | WARNING | Data load getFirstItem() method detected in loop
107 | WARNING | getFirstItem does not limit the result of collection load to one item.
Anyone has any idea how to solve it?