activerecord - 在 AR 中正确使用 AndWhere 过滤器
问题描述
我正在使用以下查询来获取总数以在 Gridview 上方显示一个小型仪表板图。
I would like to know the correct way for this query to work both when a manager is selected ( registration_id
) and when ALL (without the registration_id
parameter) is selected.
索引查看文件
$total_received_quota = Quota::find()
->where(['quota_status'=> 1])
->AndWhere(['quota_registration_id' => $manager])
->sum('quota_valor');
控制器
public function actionIndex()
{
$searchModel = new QuotaSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$manager = Yii::$app->getRequest()->getQueryParam('QuotaSearch')['quota_registration_id'];
$manager = isset($manager) ? $manager : null;
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'manager' => $manager,
]);
}
解决方案
试试这种方式:
$query= Quota::find()->where(['quota_status'=> 1]);
if(isset($manager) && $manager!=null)
{
$query->andWhere(['quota_registration_id' => $manager]);
}
$total_received_quota= $query->sum('quota_valor');
推荐阅读
- python - 查找两个数组列表之间所有不同交集和差异的 Pythonic 方法
- android - 在 Dart 运行时和原生组件之间交换数据
- html - 在文本区域中按回车时反应论坛提交
- c# - 这是什么类型的关系以及如何在 .NET Core(EFCore、FluentAPI)中实现它?
- java - 安卓。如何知道允许的块大小(蓝牙)?
- tensorflow - 如何在不结束 12 小时限制的情况下在 Google Colab 中下载 7GB 的张量流数据集?
- python-3.x - ImportError:无法从“sklearn.externals”导入名称“joblib”
- ios - 如何在原生 IOS 中调用 Method 向 Flutter 发送数据?
- perl - 在子例程中打开文件以在 Perl 中进行读/写
- asp.net-mvc - ASP.NET Core 远程验证不起作用