0

我有一个包含 2 个 mysql DATE 字段“validFrom”和“validTo”的表。

我需要允许用户搜索 validFrom 和 validTo 之间的时间段(包括)

使用搜索

  • validFrom >= 搜索的 validFrom 和
  • validTo <= 搜索到的 validTo

我必须改变什么?搜索() ?.

Gii 创建的代码告诉我这个(我认为这是一个 'LIKE %string%' 搜索)

$criteria->compare('validFromDate',$this->validFromDate,true);
$criteria->compare('validToDate',$this->validToDate,true);
4

3 回答 3

2
$criteria->addBetweenCondition('colName', $this->validFrom, $this->validTo);
于 2012-10-18T07:16:46.750 回答
1
public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $criteria->with = array('user');

        $criteria->compare('nota_id', $this->nota_id, true);
        $criteria->compare('user.nume', $this->filter_nume, true);
        $criteria->compare('persoana1', $this->persoana1, true);
        $criteria->compare('persoana2', $this->persoana2, true);

        $d = $this->getFilterDateRange($this->data_discutie);
        if (!empty($d[0]) && !empty($d[1])) {
            // between condition
            $criteria->addBetweenCondition('data_discutie', $d[0], $d[1]);
        } else if (!empty($d[0])) {
            $criteria->compare('data_discutie', '>=' . $d[0]);
        } else if (!empty($d[1])) {
            $criteria->compare('data_discutie', '<=' . $d[1]);
        }

        $d = null;
        //echo $this->urmatoarea_discutie;
        $d = $this->getFilterDateRange($this->urmatoarea_discutie);
        //print_R($d);
        if (!empty($d[0]) && !empty($d[1])) {
            // between condition
            $criteria->addBetweenCondition('urmatoarea_discutie', $d[0], $d[1]);
        } else if (!empty($d[0])) {
            $criteria->compare('urmatoarea_discutie', '>=' . $d[0]);
        } else if (!empty($d[1])) {
            $criteria->compare('urmatoarea_discutie', '<=' . $d[1]);
        }

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => '15',
                    ),
                    'sort' => array(
                        'defaultOrder' => 'coalesce(nullif(t.urmatoarea_discutie, ""), \'zzzzzz\') ASC, t.data_discutie ASC',
                        'attributes' => array(
                            'urmatoarea_discutie',
                            'data_discutie',
                            'persoana2',
                            'persoana1',
                            'filter_nume' => array(
                                'asc' => 'user.nume asc',
                                'desc' => 'user.nume desc'
                            ),
                        ),
                    ),
                ));
    }

    protected function getFilterDateRange($key) {
        $ret = array(&$from, &$to);
        switch ($key) {
            case '-1':
                // past
                $to = strtotime('next hour');
                break;
            case '1':
                // future
                $from = strtotime('last hour');
                break;
            case '-7':
                $from = strtotime('-1 week last monday midnight');
                $to = strtotime('last sunday midnight');
                break;
            case '7':
                $from = strtotime('last monday');
                $to = strtotime('next sunday midnight');
                break;
        }
        return $ret;
    }
于 2012-10-18T08:30:29.853 回答
0

根据您要执行的操作,模型中的 search() 不一定是您要查找的位置。这是一个标准的 Yii 查询:

$model = WhateverModel::model()->findAll(array(
    "condition" => "'.date('Y-m-d H:i:s' BETWEEN validRrom AND validTo).'",
));

如果您提供更详细的帮助,您将不得不提供更多详细信息:)

于 2012-10-18T07:32:16.127 回答