In the API documentation it is specified that
$joinWith
- A list of relations that this query should be joined
Please note that in addition to above awesome answers that helped me figure out how to use joinWith()
, that whenever you want to use joinWith()
and you have ambiguous column names, Yii / ActiveRecord automagically seems to pick a random column, instead of what you're usually expecting (the leftmost table). It is best to specify the leftmost table in the SELECT
clause, by specifying something like $query->select("post.*")
. I was getting ids from some inner tables and they were getting used like they were from the leftmost table, until I figured this out.
Another point to note is that you can specify an an alias for the joinwith relation, so you could say something like:
$post->find()
->joinWith(["user u"])
->where(["u.id"=>$requestedUser->id])
->select("post.*")
->orderBy(["u.created_at"=>SORT_DESC]);