1

how can I fix this error ?

**Circular dependency**: 

Heckfy\Health\Model\Metrics\CalculateOrders\Metric depends on Heckfy\Health\Model\Metrics\CalculateOrders\Metric

and vice versa

I have class metric manager where I try to init my metric object like this:

$this->metric = $this->objectManager->create('Heckfy\Health\Model\Metrics\CalculateOrders\Metric');

I guess the problem in my metric class:

class Metric extends AbstractMetric
{
 public $collectionFactory;
 public function __construct(
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory
    )
    {
        $this->collectionFactory = $collectionFactory;
    }
}

Because without constructor it works well. I tried debug it and found this part of code inside method create().

if (isset($this->creationStack[$requestedType])) {
            $lastFound = end($this->creationStack);
            $this->creationStack = [];
            throw new \LogicException("Circular dependency: {$requestedType} depends on {$lastFound} and vice versa.");
        }

Looks like magento already init this class and put it to $this->creationStack. But how?

What I am doing wrong ?

3

1 Answer 1

1

I've discussed with M2 experienced developer and He recommend me not using objectManager when we want create new object because it's bad practise. Instead of objectManager

$this->metric = $this->objectManager->create('Heckfy\Health\Model\Metrics\CalculateOrders\Metric');

we can inject this class in our constructor.

I hope it will be useful for somebody who just started learning M2

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.