0

Im trying to do a model that extends \Magento\CatalogRule\Model\Rule, but in the contructor the context is giving me a warning:

Declaration of overridden method should be compatible with parent class.

Ive seen parent class and its the same context, dont know how to fix it.

I leave the construct code here and thanks:

protected function _construct(\Magento\Framework\Model\Context $context,
                                   Product $product, Collection $collection, Iterator $iterator){
        $this->setIdFieldName('rule_id');
        $this->product = $product;
        $this->collection = $collection;
        $this->iterator = $iterator;
        parent::__construct(
            $context

        );
    }
2
  • should your construct be public as well?
    – fmsthird
    Apr 3, 2019 at 8:47
  • Public or not the problem is the same with the first param of construct Apr 3, 2019 at 8:53

2 Answers 2

0

This is the constructor for the rule model from 2.3

public function __construct(
    Context $context,
    Registry $registry,
    FormFactory $formFactory,
    TimezoneInterface $localeDate,
    CollectionFactory $productCollectionFactory,
    StoreManagerInterface $storeManager,
    CombineFactory $combineFactory,
    RuleCollectionFactory $actionCollectionFactory,
    ProductFactory $productFactory,
    Iterator $resourceIterator,
    Session $customerSession,
    Data $catalogRuleData,
    TypeListInterface $cacheTypesList,
    DateTime $dateTime,
    RuleProductProcessor $ruleProductProcessor,
    AbstractResource $resource = null,
    AbstractDb $resourceCollection = null,
    array $relatedCacheTypes = [],
    array $data = [],
    ExtensionAttributesFactory $extensionFactory = null,
    AttributeValueFactory $customAttributeFactory = null,
    Json $serializer = null,
    RuleResourceModel $ruleResourceModel = null,
    ConditionsToCollectionApplier $conditionsToCollectionApplier = null
) {
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_storeManager = $storeManager;
    $this->_combineFactory = $combineFactory;
    $this->_actionCollectionFactory = $actionCollectionFactory;
    $this->_productFactory = $productFactory;
    $this->_resourceIterator = $resourceIterator;
    $this->_customerSession = $customerSession;
    $this->_catalogRuleData = $catalogRuleData;
    $this->_cacheTypesList = $cacheTypesList;
    $this->_relatedCacheTypes = $relatedCacheTypes;
    $this->dateTime = $dateTime;
    $this->_ruleProductProcessor = $ruleProductProcessor;
    $this->ruleResourceModel = $ruleResourceModel ?: ObjectManager::getInstance()->get(RuleResourceModel::class);
    $this->conditionsToCollectionApplier = $conditionsToCollectionApplier
        ?? ObjectManager::getInstance()->get(ConditionsToCollectionApplier::class);
    parent::__construct(
        $context,
        $registry,
        $formFactory,
        $localeDate,
        $resource,
        $resourceCollection,
        $data,
        $extensionFactory,
        $customAttributeFactory,
        $serializer
    );
}

if your class extends this, you need to make your constructor receive all parameters that the original class receives and add your own if needed.
Then in your class constructor call the parent constructor with all the original parameters in the same order

Something like this:

public function __construct(
    Context $context,
    Registry $registry,
    FormFactory $formFactory,
    TimezoneInterface $localeDate,
    CollectionFactory $productCollectionFactory,
    StoreManagerInterface $storeManager,
    CombineFactory $combineFactory,
    RuleCollectionFactory $actionCollectionFactory,
    ProductFactory $productFactory,
    Iterator $resourceIterator,
    Session $customerSession,
    Data $catalogRuleData,
    TypeListInterface $cacheTypesList,
    DateTime $dateTime,
    RuleProductProcessor $ruleProductProcessor,
    //YOUR ADDITIONAL PARAMETERS SHOULD GO HERE before the optional parametrs
    AbstractResource $resource = null,
    AbstractDb $resourceCollection = null,
    array $relatedCacheTypes = [],
    array $data = [],
    ExtensionAttributesFactory $extensionFactory = null,
    AttributeValueFactory $customAttributeFactory = null,
    Json $serializer = null,
    RuleResourceModel $ruleResourceModel = null,
    ConditionsToCollectionApplier $conditionsToCollectionApplier = null
) {
    //do your custom stuff in here then....
    parent::__construct($context, $registry, $formFactory, ...., $ruleResourceModel, $conditionsToCollectionApplier); //all the original class parametrs go here, in the same order
}

Also make sure you add all the use statements for all dependencies.

3
  • Ive already tryed this and the first param whatever it was always has the same warning and error on compile: Declaration should be compatible with Rule->_construct() Apr 3, 2019 at 9:07
  • 1
    _construct or __construct 2 underscores or one?
    – Marius
    Apr 3, 2019 at 9:10
  • Okay, had only one it works now, i feel stupid but happy. Thanks. Apr 3, 2019 at 9:12
0

If you have a look at the class you are extending \Magento\CatalogRule\Model\Rule the _construct() does not inject anything, in the code you provided you are passing $context to the parent which does not need to.

Also I noticed you are calling the parent __construct() and not the _construct() which both are totally different from each other.

Plus you should not b injecting stuff into the _construct() but instead to the __construct().

3
  • Can you show me how shall i do it with code please? Apr 3, 2019 at 9:02
  • What are you trying to achieve? You might not even need those extras parameters you are trying to inject. Apr 3, 2019 at 9:05
  • I want a custom model to make some custom rules and conditions Apr 3, 2019 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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