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.
-
1Have 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.Pmclain– Pmclain2017年08月12日 12:27:31 +00:00Commented Aug 12, 2017 at 12:27
-
I am using developer mode is this still necessary? Ok yeah that seems to have fixed it howeverharri– harri2017年08月12日 12:32:20 +00:00Commented Aug 12, 2017 at 12:32
-
1Yes. 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.htmlPmclain– Pmclain2017年08月12日 12:39:42 +00:00Commented Aug 12, 2017 at 12:39
1 Answer 1
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:
- Define Variable before construct:
protected $catalogProductTypeConfigurable;
- 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
- Using the method
This then allows the use of getParentIdsByChild method by using $this->catalogProductTypeConfigurable
e.g.
$this->catalogProductTypeConfigurable->getParentIdsByChild('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.