首页 > 解决方案 > 在控制器中查找指定基本条件,但 cakephp 构建的查询与条件不匹配

问题描述

在控制器中:

$budget = $this->Budget->find('first',
        array('conditions' => array(
                "copros_id" => $this->Session->read('Copro.id'),
                "typebudgets_id" => $typebudget['Typebudget']['id'],
                "exercices_id" => $this->Session->read('Exercice.id'))));

生成sql:

SELECT `Budget`.`id`, 
       `Budget`.`created`, 
       `Budget`.`modified`, 
       `Budget`.`title`, 
       `Budget`.`statusbudgets_id`, 
       `Budget`.`typebudgets_id`, 
       `Budget`.`copros_id`, 
       `Budget`.`visible`, 
       `Budget`.`exercices_id` 
FROM `default_schema`.`budgets` AS `Budget` 
WHERE `Budget`.`typebudgets_id` = ('466b50a5-4736-11e6-a160-00163ee3b504')  

该模型包含:

public $belongsTo = array(
  'StatusBudget' => array(
    'className' => 'StatusBudget',
    'foreignKey' => 'statusbudgets_id'
   ),   
  'Exercice' => array(
    'className' => 'Exercice',
    'foreignKey' => 'exercices_id'
   ),
  'Typebudget' => array(
    'className' => 'Typebudget',
    'foreignKey' => 'typebudgets_id'
   ),   
  'Copro' => array(
    'className' => 'Copro',
    'foreignKey' => 'copros_id'
   ),   
);  

在构建查询时,cakephp (2) 似乎忽略了 find 中的条件;在查找中指定不同的条件会给出相同的 sql 作为结果。好像条件实际上并不重要。奇怪(对我来说)。谢谢

标签: mysqlcakephpfind

解决方案


这里可能会发生几件事:

  1. 首先尝试清除模型缓存。如果您不知道如何使用 cake 控制台,只需删除 /tmp/cache/model 文件夹中的文件。

  2. 模型后面的表Budget没有您要引用的列。或者他们的名字有错字。在这种情况下,Cake 不会在您构建条件时使用它们。

  3. db 表budget具有所有必需的列,但 Table的定义方式可能会干扰从数据库中正确读取表结构。


推荐阅读