我是Cakephp的新手,我试图在搜索区域中列出一些价格大于或小于值的产品并进行分页。
但是当我尝试这样做时,结果与我所期望的完全不同。当试图了解为什么会发生这种情况时,我发现我的查询将值当作String类型而不是Float处理,并且弄乱了结果。
无论我尝试什么,值的类型都不会改变,如何在cakePHP查询中更改值的类型?
// the price value that comes from the input is formatted like this: '1.000,00'
$productsModel = $this->loadModel('Products');
$this->paginate = [
'contain' => [
'Archives', 'Categories', 'Favorites'
],
'conditions' => [
//here I've tried to do some conversions manually
'Products.price >=' => floatVal(str_replace(",", ".", str_replace(".", "", $data['lower_price']))),
'Products.price <=' => floatVal(str_replace(",", ".", str_replace(".", "", $data['greater_price']))
],
'limit' => 16
];
$products = $this->paginate($productsModel);
$this->set(compact('products'));
我期望查询是这样生成的:
SELECT * FROM products WHERE price >= 29.90;
但是我有这个:
SELECT * FROM products WHERE price >= '29.90';