realtebo
realtebo

Reputation: 25691

Yii: searching for a date period

I've a table with 2 mysql DATE field 'validFrom' and 'validTo'.

I need to allow user to search for a period beetween validFrom and validTo (included)

search using

What must I change ? the search() ? .

The Gii created code tell me this (i think it's a 'LIKE %string%' search )

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

Upvotes: 0

Views: 1167

Answers (3)

Pentium10
Pentium10

Reputation: 208042

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;
    }

Upvotes: 1

Brett Gregson
Brett Gregson

Reputation: 5923

Depending on what you want to do, the search() in the model is not necessarily be the place you want to be looking. Here is a standard Yii query:

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

You will have to give a bit more detail if you more detailed help :)

Upvotes: 0

cetver
cetver

Reputation: 11829

$criteria->addBetweenCondition('colName', $this->validFrom, $this->validTo);

Upvotes: 2

Related Questions