In Magento 2.4.x, there were some changes in the constructor signatures and how collections are handled, particularly with UiComponentDataProviderSearchResult used for grids.
Steps to Fix the Issue:
Step 1: Update the Collection Class
Your Collection class extends MagentoFrameworkViewElementUiComponentDataProviderSearchResult, which means that the constructor parameters need to match the updated signature in Magento 2.4.x.
Here’s how you can update your Collection class constructor:
namespace G4AMainSliderModelResourceModelMainSliderGrid;
use MagentoFrameworkViewElementUiComponentDataProviderSearchResult;
class Collection extends SearchResult
{
protected $_idFieldName = 'slide_id';
/**
* Collection constructor.
*
* @param MagentoFrameworkDataCollectionEntityFactoryInterface $entityFactory
* @param PsrLogLoggerInterface $logger
* @param MagentoFrameworkDataCollectionDbFetchStrategyInterface $fetchStrategy
* @param MagentoFrameworkEventManagerInterface $eventManager
* @param MagentoFrameworkDBAdapterAdapterInterface|null $connection
* @param MagentoFrameworkModelResourceModelDbAbstractDb|null $resource
*/
public function __construct(
MagentoFrameworkDataCollectionEntityFactoryInterface $entityFactory,
PsrLogLoggerInterface $logger,
MagentoFrameworkDataCollectionDbFetchStrategyInterface $fetchStrategy,
MagentoFrameworkEventManagerInterface $eventManager,
?MagentoFrameworkDBAdapterAdapterInterface $connection = null,
?MagentoFrameworkModelResourceModelDbAbstractDb $resource = null
) {
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
}
}
Step 2: Update di.xml
In your di.xml, you’re defining a virtualType for your custom collection class. Since you are extending SearchResult, Magento automatically manages the database connection and other dependencies. Therefore, ensure the definition matches Magento’s expectations.
Your di.xml seems mostly fine, but ensure it looks like this:
<type name="MagentoFrameworkViewElementUiComponentDataProviderCollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="infra_main_slider_listing_data_source" xsi:type="string">G4AMainSliderModelResourceModelMainSliderGridCollection</item>
</argument>
</arguments>
</type>
<virtualType name="G4AMainSliderModelResourceModelMainSliderGridCollection" type="MagentoFrameworkViewElementUiComponentDataProviderSearchResult">
<arguments>
<argument name="mainTable" xsi:type="string">infra_main_slider</argument>
<argument name="resourceModel" xsi:type="string">G4AMainSliderModelResourceModelMainSlider</argument>
</arguments>
</virtualType>
Step 3: Clear Cache and Recompile
After making these changes, follow these steps to refresh the Magento environment:
- Clear Cache:
php bin/magento cache:flush
- Run Dependency Injection Compilation:
php bin/magento setup:di:compile
- Deploy Static Content (if in production mode):
php bin/magento setup:static-content:deploy -f
- Reindex (optional but recommended):
php bin/magento indexer:reindex
Step 4: Verify
- Test the Admin Grid to ensure that the custom module’s admin grid now works without throwing any errors.
- Confirm that the new constructor in the
Collectionclass correctly initializes the required dependencies and the grid displays as expected.
Summary:
- The error you’re encountering is due to type mismatches in the constructor parameters after upgrading Magento.
- The primary fix is ensuring that your custom
Collectionclass constructor matches the expected constructor for classes extendingSearchResultin Magento 2.4.x. - After updating the constructor and
di.xml, clear cache, recompile, and test the Admin Grid.