Yii2日期范围搜索

时间:2017-04-06 16:30:50

标签: php mysql yii2

在搜索模型中创建日期范围搜索时出现此错误。

  

完整性约束违规 - yii \ db \ IntegrityException

     

SQLSTATE [23000]:完整性约束违规:1052列   where子句中的'created_at'是不明确的

这是我的模特

 public $start_date;
    public $end_date;
public function rules()
        {
            return [
                [['attachment', 'id_client', 'delete_on', 'created_at', 'created_by', 'updated_at', 'updated_by', 'from_location', 'to_location','printed_at', 'lock','verify','verify_by','approved','approved_by'], 'integer'],
                [['policy_num'], 'autonumber', 'format'=>'formatPolicy'],
                [['policy_num','premium_policy'], 'string'],
                [['start_date','end_date'], 'date', 'format'=>'dd-MM-yyyy'],    
                [['from_location', 'to_location'], 'string', 'max' => 55],
                [['location_address'], 'string', 'max' => 100],
                [['attachment'], 'required'],
                [['deductible'], 'string', 'max' => 100],
                [['lock'], 'default', 'value' => '0'],
                [['lock'], 'mootensai\components\OptimisticLockValidator']
            ];
        }

这是我的搜索模型

    public function rules()
        {
            return [
                [['id', 'policy_num', 'attachment', 'id_client', 'delete_on','created_by', 'updated_by', 'printed_at'], 'integer'],
                [['cover_rate'], 'number'],
                [['start_date','end_date','created_at','updated_at'], 'date','format'=>'yyyy-mm-dd'],
            ];
        }

        public function search2($params)
            {
                $query = AskPolicy::find();
                $query->joinWith(['client'])->where(['id_client'=>Yii::$app->user->identity->id_client]);

                $dataProvider = new ActiveDataProvider([
                    'query' => $query,
                ]);

                $this->load($params);

            if (!$this->validate()) {
                // uncomment the following line if you do not want to return any records when validation fails
                // $query->where('0=1');
                return $dataProvider;
            }

            $query->andFilterWhere([
                'id' => $this->id,
                'policy_num' => $this->policy_num,
                'ask_policy.created_at' => $this->created_at,
                'ask_policy.updated_at' => $this->updated_at,
                'printed_at' => $this->printed_at,
            ]);

    //         $query->andFilterWhere(['>=', 'ask_policy.created_at', $this->start_date]);
//         $query->andFilterWhere(['<=', 'ask_policy.created_at', $this->end_date]);

            $query->andFilterWhere(['like',"(date_format(FROM_UNIXTIME(`created_at` ), '%Y-%m-%d' ))", $this->start_date])
            ->andFilterWhere(['like', "(date_format(FROM_UNIXTIME(`updated_at` ), '%Y-%m-%d' ))", $this->end_date]);

            return $dataProvider;
        }

如果我使用以下代码:搜索开始日期和结束日期不起作用

$query->andFilterWhere(['>=', 'ask_policy.created_at', $this->start_date]);
$query->andFilterWhere(['<=', 'ask_policy.created_at', $this->end_date]);

如何以最佳方式在Yii2中转换整数日期时间以进行日期范围搜索?我正在寻找,但没有找到很好解释的教程。

2 个答案:

答案 0 :(得分:0)

你没有给表别名试试这个

$query->andFilterWhere(['like',"(date_format(FROM_UNIXTIME(ask_policy.`created_at` ), '%Y-%m-%d' ))", $this->start_date])
->andFilterWhere(['like', "(date_format(FROM_UNIXTIME(ask_policy.`updated_at` ), '%Y-%m-%d' ))", $this->end_date]);

答案 1 :(得分:0)

您的查询与client一起加入,两个模型都有created_at字段。 您可以使用

为当前模型设置别名
$query->alias('t');

和alias使用

连接表
$query->joinWith(['client as cli']);

然后,如果您想使用主模型中的created_at,可以使用t.created_at,如果您想使用加入模型中的created_at,则可以使用cli.created_at