mysql - 在控制器中查找指定基本条件,但 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 作为结果。好像条件实际上并不重要。奇怪(对我来说)。谢谢
解决方案
这里可能会发生几件事:
首先尝试清除模型缓存。如果您不知道如何使用 cake 控制台,只需删除 /tmp/cache/model 文件夹中的文件。
模型后面的表
Budget
没有您要引用的列。或者他们的名字有错字。在这种情况下,Cake 不会在您构建条件时使用它们。db 表
budget
具有所有必需的列,但 Table类的定义方式可能会干扰从数据库中正确读取表结构。
推荐阅读
- go - image.Decode 在解码 .png 文件时返回错误
- javascript - 如果月份无效,则启用/禁用按钮
- python - scipy.integrate.solve_ivp 在 MATLAB 中完成的状态空间模拟上发散
- swift - Swift 5 - 传递给不带参数的调用的参数
- asp.net-core - .NET Core 中的 IFormFile vs byte[] 和文件上传最佳实践
- html - Pug span 无法在 flex div 中正确渲染
- typescript - rxjs 订阅一个类
- node.js - 如何使用 $set 和 filter 方法更新 mongoose 中嵌套数组中的元素?
- powershell - 使用powershell映射进程名和应用程序名
- flutter - Flutter:流和隔离