How to solve the following circular dependency error?

This is my class:

class QueueFactory extends \Magento\Framework\Model\AbstractModel
   protected $syncQueueFactory;

   public function __construct(
    QueueFactory $syncQueueFactory,
   array $data = []
   ) {
      $this->syncQueueFactory = $syncQueueFactory;
      $this->_queue = $this->syncQueueFactory->create();

Here, I want to declare syncQueueFactory of the model class QueueFactory

But if i give the same class in constructor naturally i get dependency error.

What is the alternate to use create()?

  • oh @Krishna, i just fixed that in my constructor, thought it was repeated ones as i had asked it the same very post since i dint get answer ! – Sushivam Jul 25 '16 at 7:09
  • I undeleted the post and replied the answer to the post @Krishna – Sushivam Jul 25 '16 at 7:16

You need to use an alias in your use statement:

use /Path/to/QueueFactory as QueueAlias

Then in your constructor you can do:

QueueAlias $syncQueueFactory
  • I am again getting the same error after alias name @Raphael, Circular dependency: Vendor\Module\Model\QueueFactory depends on Vendor\Module\Model\QueueFactory and vice versa. could you please check my file naming conventions here .magento.stackexchange.com/questions/126819/… – Sushivam Jul 22 '16 at 9:31
  • Have updated QueueFactory class, is my file naming conventions correct? Why data is not inserting in my custom table when i do any modifications to the product details? – Sushivam Jul 22 '16 at 9:34
  • Any update @Raphael – Sushivam Jul 22 '16 at 9:47

You should name your model class as Queue, not QueueFactory

  • I think im getting confused in the file naming conventions... can you please check my complete file structure...i have in this link magento.stackexchange.com/questions/126819/… – Sushivam Jul 22 '16 at 9:28
  • My model class name is Queue only though...but why am i not able to insert data as said in the above link – Sushivam Jul 22 '16 at 9:28
  • Any updated @kandy – Sushivam Jul 22 '16 at 9:47
  • If you add Factory suffix to any class, Magento generates factory class by self with "create" method. see devdocs.magento.com/guides/v2.0/extension-dev-guide/… for details – KAndy Jul 22 '16 at 9:48
  • So no need to add this $this->_queue = $this->syncQueueFactory->create();? – Sushivam Jul 22 '16 at 9:50

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.