2016-11-15 52 views
0

Car模型關係Brand模型由brand-id當我按關係進行搜索時,我做錯了什麼?

public function getBrand() { 
return $this->hasOne(Brand::className(), [ 
    'id' => 'brand_id' 
]); 
} 

品牌模型場。 而我想按這個字段搜索。我這樣做:

$query->joinWith('brand'); 
    $query->orFilterWhere([ 
    'like', 'brand.name', '%'.$this->company.'%', false 
]); 

搜索company字段。

品牌型號table_name是car_brand

我在做什麼錯?

回答

0

嘗試這種方式

$query->joinWith(['brand' => function ($q) { 
     $q->where('tbl_brand.name LIKE "%' . $this->company . '%"'); 
}]);