Cake PHP complex find ‘OR’ opertor is not working properly with null…
$conditions = array(
'Person.id' => array(2, 4, 1, 23, 45, 11),
'OR' => array(
array(
array('NOT' => array('Person.image' => null)),
array('NOT' => array('Person.image' => '')),
),
array(
array('NOT' => array('Person.photos' => null)),
array('NOT' => array('Person.photos' => '')),
)
)
);
The corresponding cake sql dump output query as below
SELECT `Person`.`id`, `Person`.`created`, `Person`.`modified`
FROM `people` AS `Person` WHERE `Person`.`id` IN (2, 4, 1, 23, 45, 11) AND
((((NOT (`Person`.`image` IS NULL)) AND (NOT (`Person`.`image` = NULL)))) OR
(((NOT (`Person`.`photos` IS NULL)) AND (NOT (`Person`.`photos` = '')))))
ORDER BY FIELD(`Person`.`id`, 2, 4, 1, 23, 45, 11) ASC LIMIT 3
In cake condition array, I have given Person.image is not null or '', but corresponding cake sql outputs as (NOT (Person.image IS NULL)) AND (NOT (Person.image = NULL)) where it should be like (NOT (Person.image IS NULL)) AND (NOT (Person.image = '')) .
Here Person.image both are compared with NULL itself(IS NULL and = NULL), where Person.image = NULL want to compared with empty string like Person.image = ”.
Here 'Person.image' is 'INT' and 'Person.photos' is 'VARCHAR' of type, but it is difficult to change type from current stage.
How it can be corrected ?
You are not forced to use an associative array to define conditions. You can define it like so: