0

I'm running into an error while converting below code that uses object manager to injecting within construct instead.

In this example i have this code:

$objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($product->getId());

So I want to use the Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable within the construct so assumed i would add to my current construct like below:

//1. Add variable
protected $catalogProductTypeConfigurable;
public function __construct(
 \Magento\Catalog\Block\Product\Context $context,
 \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
 \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
 \Magento\Framework\App\Http\Context $httpContext,
//2. Add Class here next to variable defined before construct 
 \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable,
 array $data = []
) {
 $this->_productCollectionFactory = $productCollectionFactory;
 $this->_catalogProductVisibility = $catalogProductVisibility;
 $this->httpContext = $httpContext;
 //3. Also add here so this class is passed to our variable
 $this->catalogProductTypeConfigurable = $catalogProductTypeConfigurable;
 parent::__construct(
 $context,
 $data
 );
}

So steps 1 sets variable, 2 takes class and passes to construct, and 3 sets this to a local variable. This then allow me to use getParentsIdsByChild function from $this->catalogProductTypeConfigurable.

e.g.

$this->catalogProductTypeConfigurable->getParentIdsByChild($product->getId());

This however throws following error:

 PHP Fatal error: Uncaught TypeError: Argument 5 passed to Vendor\\Module\\Block\\CustomBlock::__construct() must be an instance of Magento\\ConfigurableProduct\\Model\\ResourceModel\\Product\\Type\\Configurable, array given

I would like any advice on how to do this correctly.

asked Aug 12, 2017 at 12:18
3
  • 1
    Have you cleared 'var/generation'? The error looks like your new constructor argument isn't being passed, which is often caused by the generated code not yet being updated. Commented Aug 12, 2017 at 12:27
  • I am using developer mode is this still necessary? Ok yeah that seems to have fixed it however Commented Aug 12, 2017 at 12:32
  • 1
    Yes. The dev docs contain more detail about which caches should be cleared and when during development devdocs.magento.com/guides/v2.1/howdoi/php/php_clear-dirs.html Commented Aug 12, 2017 at 12:39

1 Answer 1

0

So just to tie this question up the main issue i was facing was clearing var/generation as suggested in comment my Pmclain.

Example use of object manager

$objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($product->getId());

This states that we require the use of Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable class to use the getParentIdsByChild method

Construct should be used instead of object manager directly however so follow below steps:

  1. Define Variable before construct:
protected $catalogProductTypeConfigurable;
  1. Pass class to construct and set to our variable defined in step 1:
public function __construct(
 \Magento\Catalog\Block\Product\Context $context,
 \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
 \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
 \Magento\Framework\App\Http\Context $httpContext,
 //a. Add Class here followed by variable defined in step 1 
 \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable,
 array $data = []
) {
 $this->_productCollectionFactory = $productCollectionFactory;
 $this->_catalogProductVisibility = $catalogProductVisibility;
 $this->httpContext = $httpContext;
 //b. Also add here so this class is passed to our variable
 $this->catalogProductTypeConfigurable = $catalogProductTypeConfigurable;
 parent::__construct(
 $context,
 $data
 );
}

So the class is passed through construct method and set to $this->catalogProductTypeConfigurable

  1. Using the method

This then allows the use of getParentIdsByChild method by using $this->catalogProductTypeConfigurable

e.g.

$this->catalogProductTypeConfigurable->getParentIdsByChild('1');
  1. Clear Generation folder

Changing of classes requires var/generation to be cleared otherwise you may run into issue stated in my question. Delete this by running below from your Magento root directory.

rm -rf var/generation/*

Credit to step 4 goes to Pmclain for helping me in comments.

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
answered Aug 12, 2017 at 12:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.