Yii2 从案例陈述中过滤计算字段
Yii2 Filter Calculated Field from a Case Statement
我一直在尝试对 Yii 2 中的计算案例语句列进行筛选,这就是我在搜索模型中所拥有的:
$query = Pricing::find();
$query->select("*, (CASE WHEN MyPrice > CompetitorPrice THEN 'Higher' WHEN MyPrice = CompetitorPrice THEN 'Equal' ELSE 'Lower' END) AS standing");
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
//Added Sorting to Calculated Field
$dataProvider->setSort([
'attributes' => [
'standing' => [
'asc' => ['standing' => SORT_ASC],
'desc' => ['standing' => SORT_DESC],
'label' => 'Standing',
],
]
]);
我在下面添加了这个
$query->andFilterWhere(['Standing' => $this->standing]);
但是,当我尝试针对过滤器进行搜索时,出现此错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Standing' in 'where clause'
The SQL being executed was: SELECT COUNT(*) FROM Pricing
WHERE Standing
='Lower'
为什么过滤器试图从没有 case 语句的普通 table 中获取计数?我该如何解决这个问题才能让过滤器正常工作?
我假设您正在 gridView
或类似的环境中使用此查询。 Yii 总是先做一个 count
查询,这样它就可以为视图设置适当的分页。
documentation suggests using an array if any of your select
query has commas, so it may be worth trying that, like this. You also need to specify the sort differently;
$dataProvider = new \yii\data\ActiveDataProvider([
'query' => Pricing::find()->select([
'*',
'standing' => '(CASE WHEN MyPrice > CompetitorPrice THEN 'Higher' WHEN MyPrice = CompetitorPrice THEN 'Equal' ELSE 'Lower' END)'
]),
'sort' => [
'attributes' => [
'standing' => [
'asc' => ['standing' => SORT_ASC],
'desc' => ['standing' => SORT_DESC],
'default' => SORT_DESC,
'label' => 'Standing'
]
]
]
]);
根据 回答,您还必须在模型中将别名声明为 public 变量。那应该为您排序。
public $standing;
我一直在尝试对 Yii 2 中的计算案例语句列进行筛选,这就是我在搜索模型中所拥有的:
$query = Pricing::find();
$query->select("*, (CASE WHEN MyPrice > CompetitorPrice THEN 'Higher' WHEN MyPrice = CompetitorPrice THEN 'Equal' ELSE 'Lower' END) AS standing");
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
//Added Sorting to Calculated Field
$dataProvider->setSort([
'attributes' => [
'standing' => [
'asc' => ['standing' => SORT_ASC],
'desc' => ['standing' => SORT_DESC],
'label' => 'Standing',
],
]
]);
我在下面添加了这个
$query->andFilterWhere(['Standing' => $this->standing]);
但是,当我尝试针对过滤器进行搜索时,出现此错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Standing' in 'where clause' The SQL being executed was: SELECT COUNT(*) FROM
Pricing
WHEREStanding
='Lower'
为什么过滤器试图从没有 case 语句的普通 table 中获取计数?我该如何解决这个问题才能让过滤器正常工作?
我假设您正在 gridView
或类似的环境中使用此查询。 Yii 总是先做一个 count
查询,这样它就可以为视图设置适当的分页。
documentation suggests using an array if any of your select
query has commas, so it may be worth trying that, like this. You also need to specify the sort differently;
$dataProvider = new \yii\data\ActiveDataProvider([
'query' => Pricing::find()->select([
'*',
'standing' => '(CASE WHEN MyPrice > CompetitorPrice THEN 'Higher' WHEN MyPrice = CompetitorPrice THEN 'Equal' ELSE 'Lower' END)'
]),
'sort' => [
'attributes' => [
'standing' => [
'asc' => ['standing' => SORT_ASC],
'desc' => ['standing' => SORT_DESC],
'default' => SORT_DESC,
'label' => 'Standing'
]
]
]
]);
根据
public $standing;