YII2 andFilterWhere between 不执行的问题 [ 未指定版本 ]
我写了一个GridView的DEMO,现在我想通过filter来过滤数据,其中一个特殊的要求是想要通过一个时间范围来过滤,所以有如下代码:
$query->andFilterWhere(['like', 'ORG_CODE', $this->ORG_CODE])
->andFilterWhere(['between', 'CREATED_AT', $this->START_AT, $this->END_AT]);
输入值ORG_CODE=1,START_AT=2014-01-01,END_AT=2014-01-31
希望得到的SQL:
SELECT * FROM XXX_TABLE
WHERE ORG_CODE LIKE %1%
AND (CREATED_AT BETWEEN '2014-01-01' AND '2014-01-31')
但是每次只生成:
SELECT * FROM XXX_TABLE
WHERE ORG_CODE LIKE %1%
非常奇怪,但我如果把
$query->andFilterWhere(['like', 'ORG_CODE', $this->ORG_CODE])
->andFilterWhere(['between', 'CREATED_AT', $this->START_AT, $this->END_AT]);
修改成
$query->andFilterWhere(['like', 'ORG_CODE', $this->ORG_CODE])
->andFilterWhere(['and', 'CREATED_AT', $this->START_AT, $this->END_AT]);
又能生成如下代码:
SELECT * FROM XXX_TABLE
WHERE ORG_CODE LIKE %1%
AND ((CREATED_AT) AND ('2014-01-01') AND ('2014-01-31'))
可是为什么between就不起作用呢,恳请高人指点。
共 7 个回答
-
filterWhere
和andFilterWhere
的问题。andFilterWhere Adds an additional WHERE condition to the existing one but ignores [[isEmpty()|empty operands]].The new condition and the existing one will be joined using the 'AND' operator. filterWhere Sets the WHERE part of the query but ignores [[isEmpty()|empty operands]].
共 1 条回复 -
lz19881123 回答于 2015-09-11 10:33 举报
注意一个筛选查询
$query->andFilterWhere(['like', 'ORG_CODE', $this->ORG_CODE]) ->andFilterWhere(['between', 'CREATED_AT', $this->START_AT, $this->END_AT]);//这样是错误的
$query->andFilterWhere(['like', 'ORG_CODE', $this->ORG_CODE]) ->andFilterWhere(['and', 'CREATED_AT', $this->START_AT, $this->END_AT]);//正确
-
sgj773624926 回答于 2015-11-16 16:04 举报
我使用了一下,是正确的:
User::find()->andFilterWhere(['like1', 'name', '%a%']) ->andFilterWhere(['between', 'created_at', 0, 1433088000])->all();
生成如下sql:
SELECT * FROM `user` WHERE (`name` LIKE '%a%') AND (`created_at` BETWEEN 0 AND 1433088000)
绿豆粥
注册时间:2014-08-24
最后登录:2015-02-05
在线时长:4小时54分
最后登录:2015-02-05
在线时长:4小时54分
- 粉丝2
- 金钱0
- 威望0
- 积分40