使用CakePHP查询时获得错误的结果

时间:2014-11-23 06:58:12

标签: php mysql cakephp

我正在尝试获取低库存的产品>库存。

为什么我在CakePHP查询中得到错误的结果?

使用CakePHP自定义查询它工作正常。但是使用默认的分页查询结果是错误的。

我有产品和类别模型之间的关联。

控制器代码:

$condition = array('Product.status =' => 1, 'Product.is_deleted !=' => 1, 
'Product.low_stock_alert != ' => 0, 'Product.low_stock_alert >' => 'Product.stock');

    $this->paginate = array('fields' => array('Product.id', 'Product.name',
'Product.code', 'Product.stock_type','Product.low_stock_alert',
'Product.stock','category.name'),'conditions' => $condition, 'limit' => Configure::read('LIST_PROD_NUM_RECORDS'),'order' => 'Product.id ASC');

查询生成:

SELECT `Product`.`id`, `Product`.`name`, `Product`.`code`, `Product`.`stock_type`,
`Product`.`stock`, `category`.`name` FROM `shopping`.`products` AS `Product` LEFT 
JOIN `shopping`.`categories` AS `Category` ON (`Product`.`category_id` = 
`Category`.`id`) LEFT JOIN `shopping`.`users` AS `AddedBy` ON (`Product`.`added_by` 
= `AddedBy`.`id`) LEFT JOIN `shopping`.`users` AS `ModifiedBy` ON 
(`Product`.`modified_by` = `ModifiedBy`.`id`) WHERE `Product`.`status` = 1 AND 
`Product`.`is_deleted` != 1 AND `Product`.`low_stock_alert` != 0 AND 
`Product`.`low_stock_alert` > 'Product.stock' ORDER BY `Product`.`id` ASC LIMIT 100

型号 - > Product.php

class Product extends AppModel {

    public $name = 'Product';
    public $cacheQueries = false;
    public $actsAs = array('Containable');
    public $belongsTo = array(
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'category_id',
            'fields' => array('name', 'description', 'stock_type', 'qrt_per', 'half_per', 'three_forth_per')
        ),
        'AddedBy' => array(
            'className' => 'User',
            'foreignKey' => 'added_by',
            'fields' => array('first_name', 'last_name', 'email')
        ),
        'ModifiedBy' => array(
            'className' => 'User',
            'foreignKey' => 'modified_by',
            'fields' => array('first_name', 'last_name', 'email')
        )
    );
}

型号 - > Category.php

class Category extends AppModel {
    var $name = 'Category';
    public $cacheQueries = false;
    public $belongsTo = array(
        'AddedBy' => array(
            'className' => 'User',
            'foreignKey' => 'added_by',
            'fields' => array('first_name', 'last_name', 'email')
        ),
        'ModifiedBy' => array(
            'className' => 'User',
            'foreignKey' => 'modified_by',
            'fields' => array('first_name', 'last_name', 'email')
        )
    );
    public $hasMany = array(
        'Product' => array(
            'className' => 'Product',
            'order' => 'Product.created DESC'
        )
    );
}

错误的结果:

enter image description here

但是通过以下自定义查询,我得到了准确的结果:

 $data = $this->Product->Query("SELECT products.*, categories.name, categories.description, categories.stock_type, categories.qrt_per, categories.half_per, categories.three_forth_per, AddedBy.first_name, AddedBy.last_name, AddedBy.email, ModifiedBy.first_name, ModifiedBy.last_name, ModifiedBy.email FROM sunnaair_kaziDB.products AS products LEFT JOIN sunnaair_kaziDB.categories AS categories ON (products.category_id = categories.id) LEFT JOIN sunnaair_kaziDB.users AS AddedBy ON (products.added_by = AddedBy.id) LEFT JOIN sunnaair_kaziDB.users AS ModifiedBy ON (products.modified_by = ModifiedBy.id) WHERE products.status = 1 AND products.is_deleted != 1 AND products.low_stock_alert != 0 AND products.low_stock_alert > products.stock ORDER BY products.id ASC LIMIT 50");

1 个答案:

答案 0 :(得分:3)

你的问题出在这种情况:

'Product.low_stock_alert >' => 'Product.stock'

在蛋糕中,' Product.stock'不是作为表名读取,而是作为文字字符串读取。请查看生成的SQL中的细微错误:

AND `Product`.`low_stock_alert` > 'Product.stock'

要解决此问题,请尝试将条件更改为:

'Product.low_stock_alert > Product.stock'

所以整条线变成了:

 $condition = array('Product.status =' => 1, 'Product.is_deleted !=' => 1, 
'Product.low_stock_alert != ' => 0, 'Product.low_stock_alert > Product.stock');

请记住以下内容:=>用于转义。在将列与列进行比较时,请不要使用此功能。您甚至可以使用=>一起转储以满足这些条件,因为此处没有用于SQL注入攻击的向量。