S
S
SlimSavernake2017-06-23 04:56:08
Yii
SlimSavernake, 2017-06-23 04:56:08

How to merge two ActiveDataProvider yii2 instances?

Hello. There are two entities: News and Articles. Displayed on different pages using ActiveDataProvider.
How do I combine them for use in a common list on the same page in a single ActiveDataProvider?

$dataProvider = new ActiveDataProvider([
            'query' => News::find(),
        ]);


        $dataProvider2 = new ActiveDataProvider([
            'query' => Articles::find(),
        ]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-06-23
@SlimSavernake

Good morning.
Is there any connection between articles and news? Why do you need to do it this way?
Explain what you want to achieve?
And there may be several solutions.
If there is some kind of connection between them, then it can be done through a connection or through a union of queries or through a join or something else, but how exactly - it will be clear only after your detailed explanation.
PS
You can try to change your code like this:

function actionTag($tag)
    {
        $dataProvider = new ActiveDataProvider([
            'query' => News::find()->anyTagValues($tag)->where(['status' => News::STATUS_ACTIVE]),
        ]);

       $query2 = (new \yii\db\Query())
                      ->from('articles');

      $query->union($query2);
        
        return $this->render('tag', [
            'dataProvider' => $dataProvider,
            'tag' => $tag,
        ]);
    }

See the link I gave for more details.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question