Pregunta

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

        );
    }
¿Fue útil?

Solución

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.

Otros consejos

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().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top