在控制器中执行find,指定基本条件,但是cakephp生成的查询与条件不匹配

00jrzges  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(292)

在控制器中:

$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中的条件;在find中指定不同的条件会产生相同的sql。好像条件其实并不重要。(对我来说)很奇怪。谢谢

fcipmucu

fcipmucu1#

想到伊莉亚·潘迪亚的回答,我试图更精确地说明我的情况:

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

生成的sql现在正是我所期望的:

SELECT `Budget`.`id`, ... 
FROM `default_schema`.`budgets` AS `Budget` 
  LEFT JOIN `default_schema`.`status_budgets` AS `StatusBudget` ON (`Budget`.`statusbudgets_id` = `StatusBudget`.`id`) 
  LEFT JOIN `default_schema`.`exercices` AS `Exercice` ON (`Budget`.`exercices_id` = `Exercice`.`id`) 
  LEFT JOIN `default_schema`.`typebudgets` AS `Typebudget` ON (`Budget`.`typebudgets_id` = `Typebudget`.`id`) 
  LEFT JOIN `default_schema`.`copros` AS `Copro` ON (`Budget`.`copros_id` = `Copro`.`id`)

哪里 Budget . copros_id ='5af2bda8-97d0-403a-ad96-4cf1ac171864'和 Budget . typebudgets_id ='466b50a5-4736-11e6-a160-00163ee3b504'和 Budget . exercices_id ='5af2c13b-43d0-412f-97d9-4752ac171864'极限1
谢谢您!

0yg35tkg

0yg35tkg2#

这里可能会发生一些事情:
首先尝试清除模型缓存。如果您不知道如何使用cake控制台,只需删除/tmp/cache/model文件夹中的文件。
table后面 Budget 模型没有要引用的列。或者他们的名字有错别字。在这种情况下,蛋糕不会使用他们当你建立你的条件。
的db表 budget 具有所有必需的列,但表类的定义方式可能会干扰从数据库中正确读取表结构。

相关问题