I was trying to override
<preference for="MagentoCustomerModelCustomerRegistry" type="MycustomMymoduleModelCustomerRegistry" />
under etc/di.xml. But after running the
php bin/magento setup:upgrade
command will get the ‘catalog rule indexing failed’ error message. And under the system.log file it will show the below error message –
Type Error occurred when creating object:
MagentoCustomerModelResourceModelAddressRepository,
Note: I have deleted the generated folder and run all required commands. Also, my magento version is 2.3.
2
Answers
I have had the same issue and after hours of searching I saw some of the classes can not be overridden. You can see more details about this issue here:
https://github.com/magento/magento2/issues/2674
I used magento "around" plugin instead of
<preference/>
.I had a similar issue with a preference over a class that is injected into a plugin constructor.
The problem seems to be caused by the Magento developer mode that doesn’t have all of the DI data generated at the time the plugin is executed, causing the error when executing the constructor and the DI fails.
Switching to default mode seems to fix the problem.
Anyways, I finally changed the implementation on the classes that use the overridden class in order to use a custom interface that is overridden by the preference, and use the module dependency to make my new module load after the modified one (using sequence dependency).
This is not the best solution, but it worked and I could do this because both modules are from my company, and are implemented in the app/code folder.
You should use plugins instead of preference if you can.