0

On a Magento 2 module, I try to extend

  • \Magento\Catalog\Model\Product\Option

My File : Mine/Toto/Model/Catalog/Product/Option :

namespace Mine\Toto\Model\Catalog\Product;

    class Option extends \Magento\Catalog\Model\Product\Option
    {

        const OPTION_GROUP_XFILE    = 'xfile';
        const OPTION_TYPE_XFILE     = 'xfile';

        public function __construct(
            \Magento\Framework\Model\Context $context,
            \Magento\Framework\Registry $registry,
            \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
            \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
            \Magento\Catalog\Model\Product\Option\Value $productOptionValue,
            \Magento\Catalog\Model\Product\Option\Type\Factory $optionFactory,
            \Magento\Framework\Stdlib\StringUtils $string,
            \Magento\Catalog\Model\Product\Option\Validator\Pool $validatorPool,
            \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
            \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
            array $data = []
        ) {
            parent::__construct(
                $context,
                $registry,
                $extensionFactory,
                $customAttributeFactory,
                $productOptionValue,
                $optionFactory,
                $string,
                $validatorPool,
                $resource,
                $resourceCollection,
                $data
            );
        }

        public function _construct()
        {
            $this->_init('Mine\Toto\Model\Catalog\ResourceModel\Product\Option');
            parent::_construct();
        }

        public function getGroupByType($type = null)
        {
        ...
        }

        public function groupFactory($type)
        {
        ...
        }

        public function beforeSave()
        {
        ...
        }
    }

and, as called in the _construct,

  • \Magento\Catalog\Model\ResourceModel\Product\Option

My File Mine/Toto/Model/Catalog/ResourceModel/Product/Option.php

namespace Mine\Toto\Model\Catalog\ResourceModel\Product;

    class Option extends \Magento\Catalog\Model\ResourceModel\Product\Option
    {

        public function _saveValuePrices(\Magento\Framework\Model\AbstractModel $object)
        {
        ...
        }
    }

The $this->_init('Mine\Toto\Model\Catalog\ResourceModel\Product\Option'); induces an error :

1 exception(s):
Exception #0 (Exception): Recoverable Error: Argument 1 passed to Magento\Catalog\Model\ResourceModel\Product\Option::__construct() must be an instance of Magento\Framework\Model\ResourceModel\Db\Context, instance of Magento\Framework\ObjectManager\ObjectManager given, called in /var/www/html/magento2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93 and defined in /var/www/html/magento2/vendor/magento/module-catalog/Model/ResourceModel/Product/Option.php on line 54

Thank you for your help,

0

In your extended resource model file, please try to use the following code,

 protected function _construct()
  {
    parent::_construct();
  }
| improve this answer | |
  • does Model class extends needs _construc()? I thought it was only block... – Alexglvr Jan 19 '17 at 14:34

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.