I've built an admin grid following a few tutorials and hit this error:
Warning: Invalid argument supplied for foreach() in /home/sadmin/mage/lib/internal/Magento/Framework/View/Element/UiComponent/DataProvider/DataProvider.php on line 253
I went to the file and saw this function:
protected function searchResultToOutput(SearchResultInterface $searchResult)
{
$arrItems = [];
$arrItems['items'] = [];
foreach ($searchResult->getItems() as $item) {
$itemData = [];
foreach ($item->getCustomAttributes() as $attribute) {
$itemData[$attribute->getAttributeCode()] = $attribute->getValue();
}
$arrItems['items'][] = $itemData;
}
$arrItems['totalRecords'] = $searchResult->getTotalCount();
return $arrItems;
}
for my data, there are no custom attributes, so how do I bypass this part of the function for my grid to load? Var_dumping my $searchResult->getItems()
shows my table data so I'm not sure why this is needed, any help or explanations are much appreciated :)
Thanks!
[Vendor]\[Module]\Model\ResourceModel\[Module]\Grid\Collection::__constructor
had called its' parent below this line$this->_init($model, $resourceModel);
Then I moved theparent::__constructor
to the top and issue got fixed. public function __construct( \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Store\Model\Store<virtualType>
in di.xml. So I guess when a wrong dataProvider is passed to the UiComponent it leads to this error.$searchResult->getItems()
the items where actual model instances instead of DataProvider/Model instances. I don't think that's enough for an answer but I hope this comment helps.