SQL查询在CAKE PHP 3中不起作用?

时间:2018-02-14 10:36:23

标签: cakephp-3.0

这个查询完全适用于mysql&只返回一行。

SELECT * FROM `civil_service_mileage_rates` WHERE `min_distance` <400
                                and `max_distance` >400 and `min_capacity`<1300 and `max_capacity`>1300

但是当我在cakephp3中尝试相同时,它会返回许多行数据而不是一行。

cakephp3代码:

  $civilserviceMileageTable = TableRegistry::get('civil_service_mileage_rates');
         $mileagedata = $civilserviceMileageTable->find('all');
         $claimAmount = $mileagedata->where(['min_distance'<400
                                and 'max_distance'>400 and 'min_capacity'<1300 and 'max_capacity'>1300]);

请提前帮助..谢谢

更新

上述代码的输出

 "data": [
        {
            "id": 1,
            "band": "Band 1",
            "min_distance": "0",
            "max_distance": "1500",
            "min_capacity": "0",
            "max_capacity": "1200",
            "claim_amount": 37.95
        },
        {
            "id": 2,
            "band": "Band 1",
            "min_distance": "0",
            "max_distance": "1500",
            "min_capacity": "1201",
            "max_capacity": "1500",
            "claim_amount": 39.86
        },
        {
            "id": 3,
            "band": "Band 1",
            "min_distance": "0",
            "max_distance": "1500",
            "min_capacity": "1501",
            "max_capacity": "2500",
            "claim_amount": 44.79
        }

但是,它应该只显示一行。

     "data": [

            {
                "id": 2,
                "band": "Band 1",
                "min_distance": "0",
                "max_distance": "1500",
                "min_capacity": "1201",
                "max_capacity": "1500",
                "claim_amount": 39.86
            }
}

已更新:

打印完阵列细节后,我得到了结果。

   Array
(
    [0] => Cake\ORM\Entity Object
        (
            [id] => 1
            [band] => Band 1
            [min_distance] => 0
            [max_distance] => 1500
            [min_capacity] => 0
            [max_capacity] => 1200
            [claim_amount] => 37.95
            [[new]] => 
            [[accessible]] => Array
                (
                    [*] => 1
                )

            [[dirty]] => Array
                (
                )

            [[original]] => Array
                (
                )

            [[virtual]] => Array
                (
                )

            [[errors]] => Array
                (
                )

            [[invalid]] => Array
                (
                )

            [[repository]] => civil_service_mileage_rates
        )

    [1] => Cake\ORM\Entity Object
        (
            [id] => 2
            [band] => Band 1
            [min_distance] => 0
            [max_distance] => 1500
            [min_capacity] => 1201
            [max_capacity] => 1500
            [claim_amount] => 39.86
            [[new]] => 
            [[accessible]] => Array
                (
                    [*] => 1
                )

            [[dirty]] => Array
                (
                )

            [[original]] => Array
                (
                )

            [[virtual]] => Array
                (
                )

            [[errors]] => Array
                (
                )

            [[invalid]] => Array
                (
                )

            [[repository]] => civil_service_mileage_rates
        )

    [2] => Cake\ORM\Entity Object
        (
            [id] => 3
            [band] => Band 1
            [min_distance] => 0
            [max_distance] => 1500
            [min_capacity] => 1501
            [max_capacity] => 2500
            [claim_amount] => 44.79
            [[new]] => 
            [[accessible]] => Array
                (
                    [*] => 1
                )

            [[dirty]] => Array
                (
                )

            [[original]] => Array
                (
                )

            [[virtual]] => Array
                (
                )

            [[errors]] => Array
                (
                )

            [[invalid]] => Array
                (
                )

            [[repository]] => civil_service_mileage_rates
        )
)

2 个答案:

答案 0 :(得分:1)

试试这个希望它能解决你的问题。

        $civilserviceMileageTable = TableRegistry::get('civil_service_mileage_rates');
        $mileagedata              = $civilserviceMileageTable->find();
        $mileagedata->where(['min_distance <'=> 400]);
        $mileagedata->where(['max_distance >' => 400]);
        $mileagedata->where(['min_capacity <' => 1300]);
        $mileagedata->where(['max_capacity >' => 1300]);
        $claimAmount = $mileagedata->toArray();

答案 1 :(得分:1)

试试这个

$this->civil_service_mileage_rates = TableRegistry::get('civil_service_mileage_rates');
$this->civil_service_mileage_rates->find('all')
                                  ->where([
                                         'min_distance <' => 400,
                                         'max_distance >' => 400,
                                         'min_capacity <' => 1300,
                                         'max_capacity >' => 1300,
                                    ]);
$result = $this->civil_service_mileage_rates->toArray();