0

I am trying to fetch Collection of specific ids by using filters in my custom DataProvider

    public function getData()
{
    $id =$this->request->getParam('id');//returns 5
    $answerids=$this->helper->getRelatedAnswerArray($id);//returns array with value 9 and 10 from HelperData

    $this->collection->addFieldToFilter('answer_id',$answerids);

    $arrItems = [
        'items' => [],
    ];

    foreach ($this->getCollection() as $item) {
        $arrItems['items'][] = $item->toArray([]);
    }

    return $arrItems;
}
}

the getRelatedAnswerArray() returns 9,10 in an array but the getData() returns no collection at all. But if I put the value 9,10 in an array and pass it to collection it returns the related Data.

$answerids=array(9,10);

this returns the collection but the helper data which is returning the exact same array in $answerids doesn't return any collection ...

My Helper Data Function is

    public function getRelatedAnswerArray($id)
{
    if(isset($id))
    {
        $answerRelated = $this->_answerFactory->create()->getCollection();
        $answerRelated->getSelect()
            ->joinInner(
                ['related_answer'  =>  'questionanswer'],
                'main_table.answer_id = related_answer.answer_id'
            );
        $answerRelated->addFieldToFilter('related_answer.question_id', ['eq' => $id]);
        $answerRelated->getSelect()
            ->reset(\Zend_Db_Select::COLUMNS)
            ->columns(['related_answer.answer_id']);
        foreach ($answerRelated as $key => $value)
            $postArr[]=$value->getAnswerId();
        if(!empty($postArr))
            return $postArr;
        else
            return '';
    }
}
1

try below code with in if helper return Ids in array format.

public function getData()
{
    $id =$this->request->getParam('id');//returns 5
    $answerids=$this->helper->getRelatedAnswerArray($id);//returns array with value 9 and 10 from HelperData

    $this->collection->addFieldToFilter('answer_id',['in'=>$answerids]);

    $arrItems = [
        'items' => [],
    ];

    foreach ($this->getCollection() as $item) {
        $arrItems['items'][] = $item->toArray([]);
    }

    return $arrItems;
}
}
  • It doesnot work – Ghulam.M Feb 5 at 6:07
  • I've updated my question my Helperdata Function is attached please give it a look – Ghulam.M Feb 5 at 6:09

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.