Filter relation fields

I am filtering using the _search partial view, in which i have a dropdown ‘office’ field.

So add the where clause via

$query->andFilterWhere(['=', 'property.office_id', $params['SaleSearch']['office']]);

where $params[‘SaleSearch’][‘office’] contains a simple id like ‘2’ and property table has a field ‘office_id’, contains ‘2’.

I’ve added ‘property.office_id’ to the safe rules and the query is joining via

$query = sale::find()->with([



What am i missing, as i’m getting the error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'property.office_id' in 'where clause'

The SQL being executed was: SELECT COUNT(*) FROM `sale` WHERE `property`.`office_id` = '2'

Error Info: Array


    [0] => 42S22

    [1] => 1054

    [2] => Unknown column 'property.office_id' in 'where clause'


$query = Sale::find()->joinWith(['property']);


$query->andFilterWhere(['=', 'property.office_id', $params['SaleSearch']['office']]);

Replace [color="#008800"][size=“2”]‘property.office_id’ [/size][/color]with[color="#008800"] ‘[/color]property-tablename.office_id’

whats the difference between “with” and “joinWith”, because i only put the with([‘property’]) in my initial post, but in fact there are other models in there, and using “joinWith” as you suggest breaks those others?

You have to learn how the relational queries work in Yii.

The key point is that Yii uses separated sqls for the main model and the related models.

thanks softark - this is whats confusing me - if u recall, u helped me getting this tricky situation solved:

So i’m now trying to incorporate the above related field filter, into that same functionality.

i will read and come back with more questions i suspect :)