2014-08-24 17:36:17 31867次浏览 7条回答 2 悬赏 5 金钱

我写了一个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就不起作用呢,恳请高人指点。

  • 回答于 2014-08-25 10:49 举报

    呵呵

    1 条回复
    回复于 2014-08-25 10:49 回复

    小伙儿要回答问题,灌水是要扣分的。

    , 觉得很赞
  • 回答于 2014-08-25 10:52 举报

    楼主 可以试下 addBetweenCondition

    1 条回复
    回复于 2014-08-25 19:46 回复

    我回头试试啊,可是为什么使用andFilterWhere不行呢?能解释一下吗?

  • 回答于 2014-08-25 11:01 举报

    filterWhereandFilterWhere的问题。

    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 条回复
    回复于 2014-08-25 11:05 回复

    是不是该翻译一下?

  • 回答于 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]);//正确
    
  • 回答于 2015-10-23 09:20 举报

    谁解答下这个问题啊。。。。。。。

  • 回答于 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)
    
  • 回答于 2018-05-18 19:27 举报

    2.0.15版本这个问题仍旧存在,大家解决了没有?

您需要登录后才可以回答。登录 | 立即注册
绿豆粥
试用期

绿豆粥

注册时间:2014-08-24
最后登录:2015-02-05
在线时长:4小时54分
  • 粉丝2
  • 金钱0
  • 威望0
  • 积分40

热门问题