Multiple joins

Hello everyone, I’m having a little trouble trying to dispĺay a field from a table that is not directly related to the one I’m working in.

So far I’ve managed to display different fields on the GridView using joinWith but from direct relations (following this great guide: Displaying, Sorting and Filtering Model Relations on a GridView)

I have 3 tables: “dosisxreceta”, “dosis” and “insumo”. In the “dosisxreceta” there’s a FK to “dosis”, and “dosis” has a FK referencing to “insumo” table, what I want to do is to display the “nombre_insumo” field in the “dosisxreceta” index (instead of the actual “id_dosis” which is the direct relation between “dosisxreceta” and “dosis” tables).

I’m attaching a pic of the model hoping it helps to clarify things.

Thank you.

Remember that you can easily access "nombre_insumo" like this:




$model = Dosisxreceta::find()->...one();

echo $model->dosis->insumo->nombre_insumo;



It’s just a nested relation.

For the gridView, this can be used for a column definition:




[

   'label' => 'Nombre Insumo',

   'value' => 'dosis.insumo.nombre_insumo'

],



You can optionally optimize the query for the grid using the eager loading approach.




    $query = Dosisxreceta::find();

    $query->with(['dosis', 'dosis.insumo']);

    $dataProvider = new ActiveDataProvider([

        'query' => $query,

    ]);



Thank you softark, I’m going to try what you say :)

It worked perfectly!, thank you again softark.