query

I have 3 related tables, distributors, resellers, companies

I try with php




$query1 = (new \yii\db\Query())

->select('id_rag_sociale')

->from('resellers)

->where(['fk_dis'=>1])->all();




foreach($query1 as $key)

{

    $items[] =$query2 = (new \yii\db\Query())

    ->select(['rag_sociale'])

    ->from('companies')

    ->where(['fk_res'=>$key['id_res']])->all();

    foreach ($query2 as $key2) {

          $key2['rag_sociale'];

    }

    

}




with this code have result

1 az1 NULL 1

3 az4 NULL 2

now to find the companies of the dis1 i should filter the companies table for fk_dis = 1, i get the companies of dis1, then i should do a select-> union, but i do not understand how to create a new query and pass the query results i created in code block above then merge it with the first one. all the result I will have to put it in a dataProvider with filter options

i thinhk i confused, my solution is INNER JOIN?

SELECT rag_sociale from companies where companies.fkdis = 3

UNION

SELECT rag_sociale from reseller where reseller.fk_dist = 3

UNION

SELECT companies.rag_sociale

FROM companies INNER JOIN reseller

ON reseller.id_res = companies.fk_res

WHERE reseller.fk_dis = 3

ORDER by rag_sociale ASC