Я пытаюсь получить продукт, в котором Low stock> Stock.
Почему я получаю неправильные результаты в CakePHP Query?
Используя пользовательский запрос CakePHP, он работает нормально. но с невыполнением запроса результатов разбиения на страницы.
У меня есть связь между Product и Category Model.
Код контроллера:
$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' ) ); }
Неверный результат:
Но с этим ниже пользовательским запросом я получаю точный результат:
$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");