3

I'm trying to override a model class Customer.php, maybe I've configure it wrongly, this error occurs:

 a:4:{i:0;s:316:"Recoverable Error: Argument 21 passed to Magento\Customer\Model\AccountManagement::__construct() must be an instance of Magento\Customer\Model\Customer, instance of Vendor\ModuleName\Model\Customer given in C:\xampp2\htdocs\magento2ce\vendor\magento\module-customer\Model\AccountManagement.php on line 263";i:1;s:9721:"#0 C:\xampp2\htdocs\magento2ce\vendor\magento\module-customer\Model\AccountManagement.php(263): Magento\Framework\App\ErrorHandler->handler(4096, 'Argument 21 pas...', 'C:\\xampp2\\htdoc...', 263, Array)

Here is my di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<preference for="Magento\Customer\Model\Customer" type="Vendor\ModuleName\Model\Customer" />  

</config>

I also tried using plugin, but failed too. when I use plugin, this error occurs:

Fatal error: Call to undefined method Magento\Customer\Model\ResourceModel\Customer::afterLoad() in C:\xampp2\htdocs\magento2ce\vendor\magento\framework\Model\AbstractModel.php on line 572

And my custom model class extended:

class Customer extends \Magento\Framework\Model\AbstractModel

Anyone can give me some ideas how to deal with it? Thanks

4

The preference for \Magento\Customer\Api\Data\CustomerInterface should be used in this case. Models should never be overridden in extensions, instead rely on interfaces/methods marked with @api.

The idea is that you should either use plugins on the core interface (if exists or class otherwise) or provide your custom interface implementation (via preferences). Your custom implementation should not be extended from the core classes which are not marked with @api, but it must implement core interface.

Otherwise extension will be broken with the following Magento releases.

  • Do you mean it is not recommended to override a model class? Since my extension can let users login with another password, and I've tried to view CustomerInterface.php, but it does not suit my need. I must do some checking in the Customer.php. Could you tell me how to override it or suggest me some methods? Thanks – Ricky.C Nov 23 '15 at 10:06
  • Updated answer. Also in your particular case you get fatal error, because your class is not instance of Magento\Customer\Model\Customer (does not extend it). – Alex Paliarush Nov 23 '15 at 10:13
  • @Ricky.C,alankent.me/2014/10/31/magento-2-service-contract-patterns I think this article might be helpful for you. – Valery Statichny Nov 23 '15 at 10:14
  • @ValeryStatichny it is a good article but a little bit outdated. Now data objects are mutable ( have setters and you can modify them), unlike was at the time when article was written – Alex Paliarush Nov 23 '15 at 10:19
0

if you want to extend any thing extend using plugin dont use preference if your using preference 1st thing you will miss magento core function or upgrade
the error which you mention will occur because when ever u use preference to extend and constructor should have all param mention in core or parent constructor if you miss one parama it shows error, pass correct param with same sequences
How to Override Core Block, Model and controller in Magento2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.