doufu1504 2019-06-18 12:28
浏览 19

如何进行查询比较cakephp 3.x上的浮点值?

I'm new to cakephp and I'm trying to do a search area to list some products that have their prices greater than a value, or lower than a value, and paginate it.

But when I try to do so, the result is totally different from what I've expected. When trying to get to know why was this happening, I had found out that my query was treating the value as String type instead of a Float, and it was messing up with the results.

No matter what I try, the type of value doesn't change, how do I make to change the type of the value on the query of 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'));

I was expecting that the query was generated like this:

SELECT * FROM products WHERE price >= 29.90;

But I have this instead:

SELECT * FROM products WHERE price >= '29.90';
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 uniapp连接阿里云无法发布消息和订阅
    • ¥25 麦当劳点餐系统代码纠错
    • ¥15 轮班监督委员会问题。
    • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
    • ¥20 关于变压器的具体案例分析
    • ¥15 生成的QRCode圖片加上下載按鈕
    • ¥15 板材切割优化算法,数学建模,python,lingo
    • ¥15 科来模拟ARP欺骗困惑求解
    • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
    • ¥15 unity连接Sqlserver