1

I am trying to override the class Magento\Catalog\Model\ResourceModel\Product\Collection

On overwrite class, i got the error

Fatal error: Class 'AmitBera\CoreRewrite\Model\Rewrite\Catalog\ResourceModel\Product\Select' not found in /var/www/html/magento215sample/vendor/magento/framework/Code/Generator/EntityAbstract.php on line 333

I have tried to call $this->getSelect() at a function of overwrite class

<?php
namespace AmitBera\CoreRewrite\Model\Rewrite\Catalog\ResourceModel\Product;

class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
{

    protected $appState;
    public function __construct(
        \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Eav\Model\Config $eavConfig,
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\Eav\Model\EntityFactory $eavEntityFactory,
        \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
        \Magento\Framework\Validator\UniversalFactory $universalFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Module\Manager $moduleManager,
        \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
        \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\Stdlib\DateTime $dateTime,
        \Magento\Framework\App\State $appState,
        \Magento\Customer\Api\GroupManagementInterface $groupManagement,
        \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
    ) {
        parent::__construct(
                $entityFactory,
                $logger,
                $fetchStrategy,
                $eventManager,
                $eavConfig,
                $resource,
                $eavEntityFactory,
                $resourceHelper,
                $universalFactory,
                $storeManager,
                $moduleManager,
                $catalogProductFlatState,
                $scopeConfig,
                $productOptionFactory,
                $catalogUrl,
                $localeDate,
                $customerSession,
                $dateTime,
                $groupManagement,
                $connection
            );
        $this->_appState = $appState;

    }


   public function setOrder($attribute, $dir = Select::SQL_DESC)
   {

    /* gettign the error at getSelect() */
    echo $this->getSelect()->__toString();
    exit;

    if ($attribute == 'sort_by_stocks'){
            $select =$this->getSelect();
            $select->joinLeft(
                array('stock_qty' => $this->getTable('cataloginventory/stock_status')),
                'e.entity_id = stock_qty.product_id AND stock_qty.website_id='.$this->_storeManager->getStore()->getWebsite()->getId(),
                array(
                    'salable' => 'stock_qty.stock_status',
                    'qty' => 'stock_qty.qty'
                )
            );

            $reverseDir = ($dir == 'ASC') ? 'DESC' : 'ASC';
            //this is optional - it shows the in stock products above the out of stock ones independent if sorting by position ascending or descending
           $this->getSelect()->order('salable DESC');

            return $this;
     }else{
        return parent::setOrder($attribute,$dir);   
     } 

   }
}

Question:

Why i am getting the error 'Select' not found when call at $this->getSelect() but at parent class $this->getSelect()exits but At rewrite class why we are not able to call on rewrite class`

  • It should work. But can you try this $this->getSelectSql(); and check if thats working. Because both are coming from Magento\Framework\Data\Collection\AbstractDb. – Kingshuk Deb Apr 11 '17 at 11:13
  • Also there might be something related to this class Magento\Framework\DB\Select; which is actually used to return the data getSelect(); inside Magento\Framework\Data\Collection\AbstractDb and also used inside Magento\Catalog\Model\ResourceModel\Product\Collection; – Kingshuk Deb Apr 11 '17 at 11:18
  • yes. kingshuk this getSelect should works.I have found my issue in setOrder() function parameters – Amit Bera Apr 11 '17 at 12:17
0

I found my mistake.

Issue was in parameter of setOrder().

Select class codes does not found at rewrite class but i was send it constant variable at Select::SQL_DESC at setOrder().

I have changes Select to \Magento\Framework\DB\Select and it is working

| improve this answer | |
  • Are you able to overwrite the collection methods? Actually I use the same code in Magento230, construct method is working fine but methods are not overridden. Any Idea? – Pribhav Mar 14 '19 at 7:21

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.