3

I have two custom modules and am calling one collectionfactory in other module and getting `

Call to undefined method addAttributeToSelect

Collection file look like

    class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    /**
     * Initialize resource collection
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('Vendor\Module\Model\Hello', 'Vendor\Module\Model\ResourceModel\Hello');
    }
}

and am calling in block

use Vendor\Module\Model\ResourceModel\Hello\CollectionFactory; 

    class Custom extends \Magento\Backend\Block\Widget\Grid\Extended
    {
    public function __construct(
            CollectionFactory $collectionFactory,
            array $data = []
        ) {

            $this->collectionFactory = $collectionFactory;
            parent::__construct($context, $backendHelper, $data);
        }

    protected function _prepareCollection()
        {
            $collection = $this->collectionFactory->create();
            $collection->addAttributeToSelect('*');
            $this->setCollection($collection);
            return parent::_prepareCollection();
        }

    }

Getting PHP Fatal error: Uncaught Error: Call to undefined method Vendor\Module\Model\ResourceModel\Hello\Collection::addAttributeToSelect()

2
  • 1
    Try addFieldToSelect('*')
    – PЯINCƎ
    Aug 8, 2018 at 10:52
  • Cool, I put it as an answer
    – PЯINCƎ
    Aug 8, 2018 at 11:02

2 Answers 2

9

You have to replace addAttributeToSelect('*') with addFieldToSelect('*')

addAttributeToFilter() is used to filter EAV collections.

addFieldToFilter() is used to filter Non-EAV collections.

3

Try with

$collection->addFieldToSelect('*');

which is exist under

\Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection

and

addAttributeToSelect

method exist under

\Magento\Eav\Model\Entity\Collection\AbstractCollection

so you need to create module with EAV.

1
  • Welcome!! happy to help you Please accept the answer so it will help others.
    – kunj
    Aug 8, 2018 at 11:02

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.