我正在努力使用gridview过滤器。在我的代码中,我有两个getter
public function getPhone()
{
return UserPhone::find()->where(['user_id' => $this->id])->orderBy('updated_at DESC')->one();
}
public function getDevice()
{
return ExternalAPiHelper::getDeviceInfo($this->id); // this will make a call to external db, and fetching result from there.
}
我正试图为$user->phone->country
和$user->device->type
做一个过滤器,我不确定如何以干净的方式从这两者中获得结果。目前,我正在从上面2获取id,然后使用$query->where(['in', 'id', $ids]);
中的结果数组有更好的方法吗?
usehasMany或具按要求建立关系的方法
public function getPhone(){
return $this->hasOne(UserPhone::className(),['user_id' => 'id'])
->orderBy('updated_at DESC');
}
,然后将此关系添加到要应用过滤器
的主查询中$query = ....Model query where you define above relations
$query->joinWith('phone')
$query->andWhere(['LIKE','country',$countryName])
探索更多关于yii2的关系