搜索关系数据

This must be something obvious, but I'm struggling with this for over an hour.

I have devices, laboratories and users tables. Each device is bind to a laboratory via lab_id and each laboratory is bind to users via user_id. Each device is bind to only one laboratory and each user can be bind to many laboratories.

I want to display all devices from all labs, to which current user is bind. I thought, that this will be as simple as:

$query = Device::find()->with('lab')->where($[
    'lab.user_id' => Yii::$app->user->id
]);

But this fails with error, that there is no such column as lab.user_id. What am I missing?

If you want to use related data in your query, you should simply use joinWith() instead of with() :

$query = Device::find()->joinWith('lab')->where([
    Laboratory::tableName() . '.user_id' => Yii::$app->user->id
]);