在使用yii2 Active Record查询where 多字段中有id字段,且后面继续使用andWhere后,前面的id字段过滤莫名消失的问题——方法:多增加一个只包含id字段的andWhere
$query = Order::find()->where([
'store_id' => $this->store_id,
'id' => $this->order_id,
// 'is_delete' => 0,
'is_recycle' => 0,
])
->andWhere(['id' => $this->order_id,])
->andWhere(['in', 'shop_id', explode(',', $currOwner->shop_ids)])
->with([
'user' => function ($query) {
$query->select([
'id',
'nickname' => 'from_base64(nickname)',
'binding' => "REPLACE(binding, SUBSTR(binding,4,4), '****')"
]);
}
]);
$order = $query->one();
// echo $query->createCommand()->getRawSql();
上述代码中,如果去掉andWhere([‘id’ => $this->order_id,]),用createCommand()->getRawSql()生成代码即可看到,where 中的id过滤字段消失了,不知道什么地方设置的,目前的解决办法就是增加andWhere([‘id’ => $this->order_id,])。