【发布时间】:2017-08-02 02:57:59
【问题描述】:
这是我完成过滤后的网址。
http://localhost/VMS/frontend/web/index.php?r=report%2Ffilter&ic_passport=&name=&unit_no=&category=4&purpose=
1) 我试图在 url 中获取参数
$category = Yii::$app->getRequest()->getQueryParam('category');
Yii::$app->request->getParam('category');
Yii::$app->request->get('category');
但它不起作用。我做错了什么吗?
2)假设我想根据 URL 进行查询,以便我只能导出仅被过滤的结果
`Table1::find()
->andwhere(['category_id'=>$category])
->andWhere(['visitor_name'=>$visitor_name])
->andWhere(['ic'=>$ic_passport])
->andWhere(['unit_no'=>$unit_no])
->andWhere(['purpose_id'=>$purpose])
->all(),`
根据过滤器 url,它会出现类别 4 的结果。但是当我使用自己创建的查询时,它会出现 0 结果,因为其他属性为空白。为什么在 url 中属性可以留空并且可以工作,但在查询中却不能?
更新的解决方案:
`Table1::find()
->andFilterwhere(['category_id'=>$category])
->andFilterWhere(['visitor_name'=>$visitor_name])
->andFilterWhere(['ic'=>$ic_passport])
->andFilterWhere(['unit_no'=>$unit_no])
->andFilterWhere(['purpose_id'=>$purpose])
->all()`
只需使用->andFilterWhere。
【问题讨论】: