2

this model extend Magento\Framework\Model\AbstractModel called in new controller from backend grid created with a uiComponent

public function __construct(
    \Magento\Framework\Model\Context $context, //required
    \Magento\Framework\Registry $registry, //required
    \Vnecoms\BannerManager\Model\ResourceModel\Banner $resource, //required
    \Vnecoms\BannerManager\Model\ResourceModel\Banner\Collection $resourceCollection, //required
    \Vnecoms\BannerManager\Model\BannerFactory $bannerFactory, //optional
    \Magento\Store\Model\StoreManagerInterface $storeManager, //optional
    \Magento\Framework\Logger\Monolog $monolog, //optional
    array $data = []
) {
    parent::__construct($context, $registry, $resource, $resourceCollection, $data);
    $this->_storeManager = $storeManager;
    $this->_bannerFactory = $bannerFactory;
    $this->_monolog = $monolog;
    if ($storeViewId = $this->_storeManager->getStore()->getId()) {
        $this->_storeViewId = $storeViewId;
    }
}

Everything seem ok but after i clear cache, generation Magento show error Recoverable Error: Argument 4 passed to Vnecoms\BannerManager\Model\Banner::__construct() must be an instance of Vnecoms\BannerManager\Model\ResourceModel\Banner\Collection, instance of Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult given

my di.xml

<virtualType name="Vnecoms\BannerManager\Model\ResourceModel\Banner\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
    <arguments>
        <argument name="mainTable" xsi:type="string">ves_bannermanager_banner</argument>
        <argument name="eventPrefix" xsi:type="string">ves_banner_grid_collection</argument>
        <argument name="eventObject" xsi:type="string">banner_grid_collection</argument>
        <argument name="resourceModel" xsi:type="string">Vnecoms\BannerManager\Model\ResourceModel\Banner</argument>
    </arguments>
</virtualType>

Tested in php 5.5.15 magento 2.02
How to fix it

1 Answer 1

2

You need to change virtualType name.

<virtualType name="Vnecoms\BannerManager\Model\ResourceModel\Banner\**Grid**\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
    <arguments>
        <argument name="mainTable" xsi:type="string">ves_bannermanager_banner</argument>
        <argument name="eventPrefix" xsi:type="string">ves_banner_grid_collection</argument>
        <argument name="eventObject" xsi:type="string">banner_grid_collection</argument>
        <argument name="resourceModel" xsi:type="string">Vnecoms\BannerManager\Model\ResourceModel\Banner</argument>
    </arguments>
</virtualType>

I guess virtualType redeclare your collection class.

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.