我尝试使用andFilterWhere
为mongoDB中的整数字段创建一个过滤器。如何在Yii2的整数字段中使用'andFilterWhere'和'like'?
$query->andFilterWhere(['like', 'code', $this->code ]);
此方法适用于String值,但对于整数值不起作用。如何使用此方法整数字段?
我尝试使用andFilterWhere
为mongoDB中的整数字段创建一个过滤器。如何在Yii2的整数字段中使用'andFilterWhere'和'like'?
$query->andFilterWhere(['like', 'code', $this->code ]);
此方法适用于String值,但对于整数值不起作用。如何使用此方法整数字段?
这个例子可以帮助我解决我的问题:
if ($this->code)
$query->andFilterWhere([
'$where' => '/' . $this->code . '/.test(this.code')'
]);
有或
$query->andFilterWhere(['or', ['like', 'code', $this->code ]]);
随着和
$query->andFilterWhere(['and', ['like', 'code', $this->code ]]);
对不起,此方法只能正确使用字符串值 –
谢谢,但我只是提出了如何使用整数来实现的想法。 –
如果你需要一个字符串,尝试了var
$query->andFilterWhere(['like', 'code', $this->code .'' ]);
的隐式转换或esplicit铸造或函数调用 -
$query->andFilterWhere(['like', 'code', (string)$this->code ]);
-
$query->andFilterWhere(['like', 'code', strval($this->code) ]);
最终convet你VAL在起诉前
那么如果 '代码' 是整数,你可以尝试
$query->andFilterWhere(['like', 'CAST(code as CHAR(50))', $my_value ]);
否则就可以避免andFilterWhere和使用文字和墙
if (isset($this->code) {
$query->andWhere(' CAST(code as CHAR(50)) like ' . $this->code;
}
在我的情况下''code''是整数,'$ my_value'是String。 –
我已更新anwer ..这应该解决您的问题..我知道 – scaisEdge
很抱歉,但此解决方案无法正常工作。 'CAST'在mySql中的用法 –
$query->andFilterWhere(['=', 'code', $this->code ? (int)$this->code: null]);
请解释为什么使用三元运算符,并且你是否使用int。只有代码答案并不总是非常有用。 –
为什么要为整数使用等。直接使用=运算符 –
我有整数字段的代码,我需要在gridView 中过滤包含所需代码部分的值。 –