ActiveQuery foreign table condition

I have several places where I need to generate a query that selects all the records from table A using a junction table B and the id field of the current model M, but additionaly the query should contain a condition where a field of table C has a certain value. Table C is related to A, but it seems that I can only specify conditions for A and B. To clarify this: M<—B--->A—>C

So B references M (the model with the ID that I’m using) and A, but I need to add a condition for a field in C. How can I do that?

This is what I have so far:

$query = $this->hasMany(A::className(), ["id" => "id_a"])->viaTable("B", ["id_m" => "id"]);

I know that viaTable() takes an optional callback function where I can customize the query, but I don’t know how to include table C in it.

I think you can use "joinWith()" to join a nested relation.

$query->joinWith(["a", "a.c"])->...