0

Below is my construct function of controller -

public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Vendor\Faq\Model\ResourceModel\Faq\CollectionFactory $faqCollectionFactory,
        \Vendor\Faq\Model\LikeFactory $faqLikeFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Vendor\Faq\Controller\Adminhtml\Faq\Save $save,
        \Vendor\Faq\Model\FaqFactory $faqModelFactory

    ) {
        $this->_coreRegistry = $coreRegistry;
        $this->_faqModelFactory = $faqModelFactory;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->_faqLikeFactory = $faqLikeFactory;
        $this->_customerSession = $customerSession;
        $this->_save = $save;
        parent::__construct($context);
    }

And this is my execute function -

public function execute()
    {        

        $data = $this->getRequest()->getPostValue();         
        $_faqModel = $this->_faqModelFactory->create();
        $model = $this->_faqLikeFactory->create();         
        $getFaqData = $model->load($data['faq_id'],'faq_id')->getData(); 
        print_r($getFaqData);
        die();

}

The data which is getting returned in print_r is

Array
(
    [like_id] => 83
    [user_id] => 4
    [faq_id] => 3
    [like_status] => 1
)

But the db table has 2 rows which corresponds to the table based on faq_id

The array which is formed from $data is

Array
(
    [faq_id] => 3
    [product_id] => 275
    [like_status] => 1
)
4
  • Can you please explain more what your expected result? Aug 7, 2018 at 11:57
  • I have shared image link of the db table. You can see load by faq_id = 3 should return 2 rows, but it is returning 1 row. I am doing some mistake there.
    – Gagan
    Aug 7, 2018 at 12:02
  • Image not accessible Can you please permit to access Aug 7, 2018 at 12:07
  • It is not allowing me to upload image, sorry but the point is that corresponding to that faq_id I have 2 rows in db but array returns 1 row.
    – Gagan
    Aug 7, 2018 at 12:11

2 Answers 2

0

Try this:

public function execute()
{        

    $data = $this->getRequest()->getPostValue();         
    $collection = $this->_faqModelFactory->create()->getCollection();
    $collection = $collection->addFieldToFilter('faq_id', $data['faq_id']);

    print_r($collection->getData()); die();

}
1
  • Thanks but Isn't this possible by load? I want to use that method.
    – Gagan
    Aug 7, 2018 at 12:11
0

I have managed to get the answer myself.

Their were several changes needed to make it work.

1) This can not be done by load but by collection. It seems that load is used to fetch single query. I am still not sure for that but quite confident. So I was to use the collection.

2) In order to use the collection I was to make a file Collection.php which will reside at the location Vendor\Faq\Model\ResourceModel\Like\Collection

3) The code of the Collection should extend to \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection along with the need to include the construct init with model and resourceModel of Like

4) Now inject the Collection in the construct of the controller like

\Vendor\Faq\Model\ResourceModel\Like\Collection $likeCollectionFactory,

5) Then using getCollection fetch the data -

        $collection = $this->_faqLikeFactory->create()->getCollection();
        $collection = $collection->addFieldToFilter('faq_id', $data['faq_id']);
        print_r($collection->getData());

If anyone has better way to get data please share your answer/comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.