user3340220
user3340220

Reputation: 1

In the controller do a find specifiing basic conditions but the query built by cakephp doesn't match the conditions

In the controller :

$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'))));

generates the 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')  

The Model contains :

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'
   ),   
);  

It looks like the conditions in the find are ignored by cakephp (2) when building the query; specifying different conditions in the find give the same sql as a result. As if the conditions don't matter in fact. Strange (for me). Thanks

Upvotes: 0

Views: 44

Answers (2)

user3340220
user3340220

Reputation: 1

Thinking about Ilia Pandia's answer I tried to specify more precisly my conditions:

   $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'))));

The sql generated is now what I expected:

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`) 

WHERE Budget.copros_id = '5af2bda8-97d0-403a-ad96-4cf1ac171864' AND Budget.typebudgets_id = '466b50a5-4736-11e6-a160-00163ee3b504' AND Budget.exercices_id = '5af2c13b-43d0-412f-97d9-4752ac171864' LIMIT 1

Thank you!

Upvotes: 0

Ilie Pandia
Ilie Pandia

Reputation: 1839

A couple of things may be happening here:

  1. First try to clear the model cache. If you don't know how to use the cake console just delete the files in the /tmp/cache/model folders.

  2. The table behind the Budget model does NOT have the columns you are making a reference to. Or there is a typo in their name. In that case Cake will not use them when you build your conditions.

  3. The db table for budget has all the required columns, but the way the Table class is defined could interfere with properly reading the table structure from the database.

Upvotes: 1

Related Questions