0

I'm doing "Banner Slider", and I'm following module-catalog/category.
So when i trying to copy this file:

C:\xampp\htdocs\magento\vendor\magento\module-catalog\Model\ResourceModel\Product\Collection.php

I found that class was extends this class \Magento\Eav\Model\Entity\Collection\AbstractCollection.
I know we should extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection.
But when i try to follow them, it throw an error of memory when i using the init() function like this:

protected function _construct()
{
    $this->_init(\Aht\BannerSlider\Model\Slide::class, \Aht\BannerSlider\Model\ResourceModel\Slide::class);
}

Here is my

C:\xampp\htdocs\magento\app\code\Aht\BannerSlider\Model\ResourceModel\Slide\Collection.php


<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 13/07/2018
 * Time: 3:31 CH
 */

namespace Aht\BannerSlider\Model\ResourceModel\Slide;


// extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection \Magento\Eav\Model\Entity\Collection\AbstractCollection
class Collection extends \Aht\BannerSlider\Model\ResourceModel\Collection\AbstractCollection
{
protected $_idFieldName = 'id';

/**
 * Define resource model
 *
 * @return void
 */
protected function _construct()
{
    $this->_init(\Aht\BannerSlider\Model\Slide::class, \Aht\BannerSlider\Model\ResourceModel\Slide::class);
}

public function addAttributeToSelect($attribute, $joinType = false)
{
    return parent::addAttributeToSelect($attribute, $joinType);
}
}


Here is my

C:\xampp\htdocs\magento\app\code\Aht\BannerSlider\Model\ResourceModel\Collection\AbstractCollection.php


<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Aht\BannerSlider\Model\ResourceModel\Collection;

/**
 * Catalog EAV collection resource abstract model
 * Implement using different stores for retrieve attribute values
 *
 * @api
 * @author      Magento Core Team <core@magentocommerce.com>
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @since 100.0.2
 */
class AbstractCollection extends \Magento\Eav\Model\Entity\Collection\AbstractCollection
{
    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\Eav\Model\ResourceModel\Helper $resourceHelper,
        \Magento\Framework\Validator\UniversalFactory $universalFactory,
        \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
    ) {
        parent::__construct(
            $entityFactory,
            $logger,
            $fetchStrategy,
            $eventManager,
            $eavConfig,
            $resource,
            $eavEntityFactory,
            $resourceHelper,
            $universalFactory,
            $connection
        );
    }
}
  • WHy you don't use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection ? – Amit Bera Aug 3 '18 at 8:09
  • because the \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection dont have addAttributeToSelect() function. I need it in the copy of file C:\xampp\htdocs\magento\vendor\magento\module-catalog\Block\Adminhtml\Category\Tab\Product.php – fudu Aug 3 '18 at 8:16
  • I've create a same file as "Product.php" and named it "Slide.php" for the same content. – fudu Aug 3 '18 at 8:17
  • Even if it doesn't have addAttributeToFIlter, you can always use addFieldToFilter to filter your collection. – Vivek Kumar Feb 14 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.