yii如何在queryall中选择日期范围

时间:2014-07-03 13:07:05

标签: yii

这是我的查询

$query= Yii::app()->db->createCommand()
                    ->select('*,SUM(amount) AS TotalItemsOrdered')
                    ->from('shoppinglist')
                    ->where('user_type="Admin" ')
                    ->group('cat_id,ing_id,measure_id')
                    ->queryAll();

我从今天开始,我在当前日期添加了6天以获得最后日期,即完整的一周假设       $ startdate =" 7-3-14&#34 ;; //月日年         $ enddate =" 7-7-14&#34 ;; 我如何使用此代码Date BETWEEN DATE('$DateFrom_order') AND DATE('$DateTo_order') 或者我在mysql的声明中使用,但如何使用或嵌入上面的YII查询 编辑 这是我的模特

<?php

类Shoppinglist扩展了CActiveRecord {

public static function model($className=__CLASS__)
{
    return parent::model($className);
}

public function tableName()
{
    return 'shoppinglist';
}

/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('ing_id, measure_id, amount, user_id, cat_id, date_added, user_type', 'required'),
        array('ing_id, measure_id, amount, user_id, cat_id', 'numerical', 'integerOnly'=>true),
        array('user_type', 'length', 'max'=>5),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, ing_id, measure_id, amount, user_id, cat_id, date_added, user_type', 'safe', 'on'=>'search'),
    );
}

/**
 * @return array relational rules.
 */
public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
    );
}

/**
 * @return array customized attribute labels (name=>label)
 */
public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'ing_id' => 'Ing',
        'measure_id' => 'Measure',
        'amount' => 'Amount',
        'user_id' => 'User',
        'cat_id' => 'Cat',
        'date_added' => 'Date Added',
        'user_type' => 'User Type',
    );
}

/**
 * Retrieves a list of models based on the current search/filter conditions.
 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
 */
public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('ing_id',$this->ing_id);
    $criteria->compare('measure_id',$this->measure_id);
    $criteria->compare('amount',$this->amount);
    $criteria->compare('user_id',$this->user_id);
    $criteria->compare('cat_id',$this->cat_id);
    $criteria->compare('date_added',$this->date_added,true);
    $criteria->compare('user_type',$this->user_type,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

}

1 个答案:

答案 0 :(得分:3)

如果数据库中的字段日期为DATE(类型),则为:

$query= Yii::app()->db->createCommand()
            ->select('*,SUM(amount) AS TotalItemsOrdered')
            ->from('shoppinglist')
            ->where("user_type='Admin' AND Date BETWEEN STR_TO_DATE( '$startdate', '%d-%m-%y' ) AND STR_TO_DATE( '$enddate', '%d-%m-%y' )")
            ->group('cat_id,ing_id,measure_id')
            ->queryAll();