2

Interception cache generation... 6/7 [========================>---] 85% 50 secs 351.0 MiBErrors during compilation:

Vendor\Module\Model\ResourceModel\Brand\Collection Incompatible argument type: Required type: \Magento\Framework\DB\Adapter\AdapterInterface. Actual type: \Magento\Framework\Model\ResourceModel\Db\AbstractDb; File: /var/www/html/app/code/Vendor/Module/Model/ResourceModel/Brand/Collection.php

Total Errors Count: 1

In Log.php line 92: Error during compilation

setup:di:compile [] []

<?php

namespace Vendor\Module\Model\ResourceModel\Brand;

 class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection {
/**
 * store view id
 * @var int
 */
protected $_storeViewId = null;

/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
protected $_storeManager;

protected $_addedTable = [];

protected function _construct() {
    $this->_init('Vendor\Module\Model\Brand', 'Vendor\Module\Model\ResourceModel\Brand');
}

/**
 * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
 * @param \Psr\Log\LoggerInterface $logger
 * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
 * @param \Magento\Framework\Event\ManagerInterface $eventManager
 * @param \Zend_Db_Adapter_Abstract $connection
 * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
 */
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,
    $connection = null,
    \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
    parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection);
    $this->_storeManager = $storeManager;

    if ($storeViewId = $this->_storeManager->getStore()->getId()) {
        $this->_storeViewId = $storeViewId;
    }
}

/**
 * get store view id
 * @return int [description]
 */
public function getStoreViewId() {
    return $this->_storeViewId;
}

/**
 * set store view id
 * @param int $storeViewId [description]
 */
public function setStoreViewId($storeViewId) {
    $this->_storeViewId = $storeViewId;
    return $this;
}

/**
 * Multi store view
 * @param string|array $field
 * @param null|string|array $condition
 */
public function addFieldToFilter($field, $condition = null) {
    $attributes = array(
        'name',
        'status',
        'click_url',
        'image_alt',
        'store_id',
    );
    $storeViewId = $this->getStoreViewId();
    if (in_array($field, $attributes) && $storeViewId) {
        if (!in_array($field, $this->_addedTable)) {
            $this->getSelect();
            $this->_addedTable[] = $field;
        }
        // return parent::addFieldToFilter("IF($field.value IS NULL, main_table.$field, $field.value)", $condition);
        return parent::addFieldToFilter($field, $condition);
    }
    if ($field == 'store_id') {
        $field = 'main_table.brand_id';
    }
    return parent::addFieldToFilter($field, $condition);
}

/**
 * Multi store view
 */
protected function _afterLoad() {
    parent::_afterLoad();
    if ($storeViewId = $this->getStoreViewId()) {
        foreach ($this->_items as $item) {
            $item->setStoreViewId($storeViewId)->getStoreViewValue();
        }
    }
    return $this;
}

/**
 * set order random by brand id
 *
 * @return $this
 */
public function setOrderByBrand()
{
    $this->getSelect()->order('order');

    return $this;
}

}

1

This is the constructor of the parent class

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\Framework\DB\Adapter\AdapterInterface $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    ) {

In your class constructor change the line

$connection = null,

To

\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,

Then add $resource in your parent construct so it look like this and match the class needed for the parent constructor

parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection,$resource);

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.