2

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()

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

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

addAttributeToFilter() is used to filter EAV collections.

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

2

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.

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

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.