2

any solution for this bug

 [RuntimeException]                                                           
  Class Vendor\Company\Model\PostFactory does not exist in [Vendor\Company\Controller\Index\Index\Interceptor]    
  1. block post.php
<?php

namespace Vendor\Company\Block;

use Magento\Framework\View\Element\Template;
use Magento\Backend\Block\Template\Context;

class Campaign extends Template
{
    public function __construct(Context $context,array $data = [])
    {
        parent::__construct($context, $data);
    }
    public function getFormAction()
        {
        return $this->getUrl('vendor/index/submit', ['_secure' => true]);  
    }
}

2 model/resourceModel/post.php

<?php

namespace Vendor\Company\Model\ResourceModel;

class Submit extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    
    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context
    )
    {
        parent::__construct($context);
    }
    
    protected function _construct()
    {
        $this->_init('vendor_company', 'campaign_id');
    }
    
}
  1. Controller post.php
<?php

namespace Vendor\Company\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;

    protected $_postFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Vendor\Company\Model\PostFactory $postFactory
        )
    {
        $this->_pageFactory = $pageFactory;
        $this->_postFactory = $postFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        $post = $this->_postFactory->create();
        $collection = $post->getCollection();
        foreach($collection as $item){
            echo "<pre>";
            print_r($item->getData());
            echo "</pre>";
        }
        exit();
        return $this->_pageFactory->create();
    }
}
  1. Model post.php
<?php

namespace Vendor\Company\Model;

class Submit extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{
    const CACHE_TAG = 'vendor_company';

    protected $_cacheTag = 'vendor_company';

    protected $_eventPrefix = 'vendor_company';

    protected function _construct()
    {
        $this->_init('Vendor\Company\Model\ResourceModel\Submit');
    }

    public function getIdentities()
    {
        return [self::CACHE_TAG . '_' . $this->getId()];
    }

    public function getDefaultValues()
    {
        $values = [];

        return $values;
    }
}
7
  • can you please share that code so that i can give you exact solution Sep 23, 2020 at 9:38
  • sure i will show my code Sep 23, 2020 at 9:45
  • have you check by remove generated and var/cache folder ? Sep 23, 2020 at 10:04
  • i have checked generated and var/cache folder Sep 23, 2020 at 10:15
  • have you check permission of generated and var , if not than set it to 777 Sep 23, 2020 at 10:18

1 Answer 1

3

The Vendor\Company\Model\PostFactory Class is injected at Vendor\Company\Controller\Index\Index (in the constructor) and from there the error occurs.

If you have Vendor\Company\Model\Post Class then during di (Dependency Injection) compilation Vendor\Company\Model\PostFactory should automatically be created.

There might be two reason the issue happens:

  • Vendor\Company\Model\Post does not exit.
  • Or Dependency Injection does not know about the file yet.
    • Delete all files& folder from generate/code, generate/metadata and run:
   php bin/magento setup:di:compile

You have two options regarding Vendor/Company/Model/Post:

  • If Vendor\Company\Model\Post does not exits then you have to remove Vendor\Company\Model\PostFactory from Vendor\Company\Controller\Index\Index constructor.

  • Or create new Vendor\Company\Model\Post and run the di compilation.

(Depends on your requirements which is best)

1
  • Vendor\Company\Model\Post it is existed in my module Sep 23, 2020 at 9:36

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.