I'm using the Yii2 class yii\elasticsearch\ActiveRecord to recieve data from elastic search. Usually methods in this class for getting data from elastic will return that data as ActiveRecord (AR) object. So it's easy, to create an activeDataProvider from that AR to fill that data into a listview, etc.
But: yii\elasticsearch\ActiveRecord::mget() does not return an AR-object. Instead it return an array of documents.
My questions:
1.) Is there a way to use the mget - feature / elastic multi get feature AND get the result as AR object?
OR
2.) Is there a way to bring that array of documents into an AR object to make ActiveDataProvider including listview working?
CodePudding user response:
I found the following solution:
- Runnig Yii2 mget with an list of the ID's I like to get, which returns an array of docs.
- Creating an ActiveDataProvider using ArrayDataProvider Class and the mget-output from step before
Example Code:
use app\models\MyModel;
use yii\data\ArrayDataProvider;
$ids = ['123','456','789'];
$myModel = new MyModel(); # Data Model based on yii\mongodb\ActiveRecord;
$result = $myModel->mget($ids); # get documents from elastic where document id is in $ids
$dataProvider = new ArrayDataProvider([
'allModels' => $result
]); # creating ActiveDataProvider, which can be used in Listviews.