首页 > 解决方案 > 如何在 cakephp 3.X 的选择查询中键入强制转换数据库字段?

问题描述

在我的表中,我有一个十进制的数量字段。数据库中“数量”的值为 1.00,但是当我从查询中选择它时,它给了我整数 1。但是,如果该值为 1.25,它给出相同的浮点值 1.25。这是我的查询和输出 -

询问

$app_cart = $this->AppCart->find()->select(['AppCart.id','item'=>'Items.name','price'=>'Items.sales_rate','quantity'=>'AppCart.quantity'])
        ->where(['AppCart.user_id'=>1])->contain(['Items']);

输出

{
"success": true,
"message": "List Found",
"app_cart": [
    {
        "id": 4,
        "item": "KANGAN SAREE",
        "price": 1375,
        "quantity": 1
    },
    {
        "id": 5,
        "item": "KANGAN SAREE",
        "price": 1375,
        "quantity": 1
    }
]}

我努力了

$app_cart
        ->selectTypeMap()
        ->addDefaults([
            'quantity' => 'decimal'
        ]);

标签: phpcakephpcakephp-3.0

解决方案


推荐阅读