1

I have below error while running

php bin/magento setup:di:compile

Errors during compilation:
        Custom\Module\Block\BuyingListIndex
                Incorrect dependency in class Custom\Module\Block\BuyingListIndex in D:/wamp/www/smint/magento/app/code/Custom/Module/Block/BuyingListIndex.php
\Magento\Framework\Filesystem already exists in context object
\Magento\Framework\App\Config\ScopeConfigInterface already exists in context object
        Custom\Module\Block\CategoryListIndex
                Incorrect dependency in class Custom\Module\Block\CategoryListIndex in D:/wamp/www/smint/magento/app/code/Custom/Module/Block/CategoryListIndex.php
\Magento\Framework\Filesystem already exists in context object
\Magento\Framework\App\Config\ScopeConfigInterface already exists in context object
        Custom\Module\Block\EtradeIndex
                Incorrect dependency in class Custom\Module\Block\EtradeIndex in D:/wamp/www/smint/magento/app/code/Custom/Module/Block/EtradeIndex.php
\Magento\Framework\Filesystem already exists in context object
        Custom\Module\Block\MyModule\BuyingList
                Incorrect dependency in class Custom\Module\Block\MyModule\BuyingList in D:/wamp/www/smint/magento/app/code/Custom/Module/Block/MyModule/BuyingList.php
\Magento\Framework\Filesystem already exists in context object
        Custom\Module\Block\MyModule\Edit
                Incorrect dependency in class Custom\Module\Block\MyModule\Edit in D:/wamp/www/smint/magento/app/code/Custom/Module/Block/MyModule/Edit.php
\Magento\Framework\Filesystem already exists in context object
        Custom\Module\Block\MyModule\EditMessage
                Incorrect dependency in class Custom\Module\Block\MyModule\EditMessage in D:/wamp/www/smint/magento/app/code/Custom/Module/Block/MyModule/EditMessage.php
\Magento\Framework\Filesystem already exists in context object
        Custom\Module\Block\MyModule\Index
                Incorrect dependency in class Custom\Module\Block\MyModule\Index in D:/wamp/www/smint/magento/app/code/Custom/Module/Block/MyModule/Index.php
\Magento\Framework\Filesystem already exists in context object
        Custom\Module\Block\Posts\ViewPosts
                Incorrect dependency in class Custom\Module\Block\Posts\ViewPosts in D:/wamp/www/smint/magento/app/code/Custom/Module/Block/Posts/ViewPosts.php
\Magento\Framework\Filesystem already exists in context object
\Magento\Framework\App\Config\ScopeConfigInterface already exists in context object
        Custom\Module\Block\SellingListIndex
                Incorrect dependency in class Custom\Module\Block\SellingListIndex in D:/wamp/www/smint/magento/app/code/Custom/Module/Block/SellingListIndex.php
\Magento\Framework\Filesystem already exists in context object
\Magento\Framework\App\Config\ScopeConfigInterface already exists in context object
        Custom\Module\Block\UserPostsListIndex
                Incorrect dependency in class Custom\Module\Block\UserPostsListIndex in D:/wamp/www/smint/magento/app/code/Custom/Module/Block/UserPostsListIndex.php
\Magento\Framework\Filesystem already exists in context object
\Magento\Framework\App\Config\ScopeConfigInterface already exists in context object
        Custom\Module\Model\ResourceModel\Messages\Grid\Collection
                Incompatible argument type: Required type: \Magento\Framework\DB\Adapter\AdapterInterface. Actual type: \Magento\Store\Model\StoreManagerInterface; File:
D:/wamp/www/smint/magento/app/code/Custom/Module/Model/ResourceModel/Messages/Grid/Collection.php

        Custom\Module\Model\ResourceModel\Posts\Grid\Collection
                Incompatible argument type: Required type: \Magento\Framework\DB\Adapter\AdapterInterface. Actual type: \Magento\Store\Model\StoreManagerInterface; File:
D:/wamp/www/smint/magento/app/code/Custom/Module/Model/ResourceModel/Posts/Grid/Collection.php

Total Errors Count: 12

D:/wamp/www/smint/magento/app/code/Custom/Module/Block/BuyingListIndex.php Code

namespace Custom\Module\Block;

class BuyingListIndex extends \Magento\Framework\View\Element\Template {

    public function __construct(
    \Magento\Framework\View\Element\Template\Context $context, \Custom\Module\Model\ResourceModel\Posts\CollectionFactory $collectionFactory, \Custom\Module\Model\Posts $posts, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Image\AdapterFactory $imageFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, array $data = []
    ) {
        $this->collectionFactory = $collectionFactory;
        $this->posts = $posts;
        $this->_filesystem = $filesystem;
        $this->_imageFactory = $imageFactory;
        $this->scopeConfig = $scopeConfig;
         parent::__construct($context, $data);
    }
}

[UPDATE]

app\code\Custom\Module\Model\ResourceModel\Posts\Grid\Collection.php

namespace Custom\Module\Model\ResourceModel\Posts\Grid;

use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Search\AggregationInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Custom\Module\Model\ResourceModel\Posts\Collection as PostsCollection;

/**
 * Collection for displaying grid of etrade posts
 */
class Collection extends PostsCollection implements SearchResultInterface
{
    public function __construct(
        \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\EntityManager\MetadataPool $metadataPool,
        $mainTable,
        $eventPrefix,
        $eventObject,
        $resourceModel,
        $model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document',
        $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    ) {
        parent::__construct(
            $entityFactory,
            $logger,
            $fetchStrategy,
            $eventManager,
            $storeManager,
            $metadataPool,
            $connection,
            $resource
        );
        $this->_eventPrefix = $eventPrefix;
        $this->_eventObject = $eventObject;
        $this->_init($model, $resourceModel);
        $this->setMainTable($mainTable);
    }
}

How to solve above complication errors?

2 Answers 2

5

Try with below code,

Remove var folder and run command of compile,

class BuyingListIndex extends \Magento\Framework\View\Element\Template {
    public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Custom\Module\Model\ResourceModel\Posts\CollectionFactory $collectionFactory, 
    \Custom\Module\Model\Posts $posts,
    \Magento\Framework\Image\AdapterFactory $imageFactory,
    array $data = []
    ) {
        $this->collectionFactory = $collectionFactory;
        $this->posts = $posts;
        $this->_filesystem = $context->getFilesystem();
        $this->_imageFactory = $imageFactory;
        $this->scopeConfig = $context->getScopeConfig();
         parent::__construct($context, $data);
    }
}
6
  • try with updated code Oct 18, 2016 at 8:56
  • Hi @Rakesh. Seems works. Let me check for All files :)
    – Jackson
    Oct 18, 2016 at 9:48
  • Custom\Module\Model\ResourceModel\Messages\Grid\Collection Incompatible argument type: Required type: \Magento\Framework\DB\Adapter\AdapterInterface. Actual type: \Magento\Store\Model\StoreManagerInterface; File: D:/wamp/www/smint/magento/app/code/Custom/Module/Model/ResourceModel/Messages/Grid/Collection.php for this?
    – Jackson
    Oct 18, 2016 at 10:14
  • which error are display after updating above code? Oct 18, 2016 at 10:37
  • Hi @Rakesh. Your code is working fine. I have total 12 Errors. 11 are solved by ur code. 1 is remaining which i posted in Comment. Thanks
    – Jackson
    Oct 19, 2016 at 1:22
4

That's the beauty of dependency injection that you don't end up using same class multiple times :)

Look at the following two classes and you will find all the functions you need

vendor/magento/framework/View/Element/Template/Context.php

vendor/magento/framework/View/Element/Context.php

For example you can call the followings in your constructor

$context->getLogger() which means you don't need \Psr\Log\LoggerInterface $logger

or

$context->getStoreManager() which means you don't need \Magento\Store\Model\StoreManagerInterface

1
  • 1
    This is actually the better answer. It's important to understand why something goes wrong, rather than only getting a solution without understanding the error. The problem here is that your constructor includes a dependency that's already provided in the context object. Magento detects this and throws an error, thus preventing you from loading the identical class twice and wasting precious resources. Apr 4, 2018 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.