1

I am trying to filter the data from my custom table, But it is throwing Error.

Fatal error: Cannot redeclare class Test\Learning\ResourceModel\Learning\Collection in C:\xampp\htdocs\demo\app\code\Test\Learning\Model\ResourceModel\Learning\Collection.php on line 24

Block/Learning.php

<?php
namespace Test\Learning\Block;


class Learning extends \Magento\Framework\View\Element\Template
{


    /**
     * @var \Test\Learning\Model\LearningFactory
     */
    protected  $_learningFactory;


    public function __construct
    (
        \Magento\Framework\View\Element\Template\Context $context,
        \Test\Learning\Model\LearningFactory $learningFactory,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->_learningFactory = $learningFactory;

    }

    public function getFilterData(){


        $customerId = "1";
        $data = $this->_learningFactory->create()->getCollection()->addFieldToFilter(array('customer_id',$customerId));

        foreach ($data as $k => $v) {

        }       

        return $data;
    }

}

Model/Learning/ResourceModel/Learning/Collection.php

<?php
namespace Test\Learning\ResourceModel\Learning;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{

    /**
     * @var string
     */
    protected $_idFieldName = 'clul_id';

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Test\Learning\Model\Learning', 'Test\Learning\Model\ResourceModel\Learning');
        $this->_map['fields']['clul_id'] = 'main_table.clul_id';
    }


}

/view/frontend/templates/learning.phtml

<?php echo $block->getFilterData(); ?>

Any help On this?

  • 1
    Namespace is incorrect use Test\Learning\Model\ResourceModel\Learning instead Test\Learning\ResourceModel\Learning in Collection.. – Mohammad Mujassam Dec 15 '16 at 10:19
4

your collection class needs to be in the file Test/Learning/Model/ResourceModel/Learning/Collection.php and have the namespace namespace Test\Learning\Model\ResourceModel\Learning;

  • i think it ok? why you used "\" before at class Collection? – Nguyen Cong Binh Dec 15 '16 at 10:16
  • Sorry for the extra backslash. – Marius Dec 15 '16 at 10:30
0

You have to keep below line in your function,

$data = $this->_learningFactory->create()->getCollection()->addFieldToFilter('customer_id', array('eq' => $customerId));

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.