I am using Cakephp 2.10.0. While working with find condition, I am facing a strange issues LIKE "OR" condition convert automatically to "AND" e.g
I am making find condition like:
$this->{$this->leadCall}->find('list', array('conditions'=>array($this->leadCall.'.future_call !=' => date('m/d/Y'), 'or' => array($this->leadCall.'.future_call !=' => '')),'fields' => array('lead_id')));
It results
SELECT `LeadCall`.`id`, `LeadCall`.`lead_id` FROM `zindagihomes`.`lead_calls` AS `LeadCall` WHERE `LeadCall`.`future_call` != '11/19/2017' AND `LeadCall`.`future_call` != ''
And I want query something like this
SELECT `LeadCall`.`id`, `LeadCall`.`lead_id` FROM `zindagihomes`.`lead_calls` AS `LeadCall` WHERE `LeadCall`.`future_call` != '11/19/2017' OR `LeadCall`.`future_call` != ''
I got my mistake. It was due wrong array formation.
<pre>
$conditions = array(
'OR' => array(
$this->leadCall.'.future_call !=' => date('m/d/Y'),
$this->leadCall.'.future_call !=' => ''
)
);
$futureCall = $this->{$this->leadCall}->find('list', array('conditions'=>$conditions,'fields' => array('lead_id')));
</pre>
it is working fine as expected.
Related
Im looking to create a simple query but i can't seem to figure out how to do it using Cakes conditions (cake 1.3), what im looking to do is quite simple, in SQL its just
SELECT * FROM table WHERE firstvalue != '' OR secondvalue != ''
So basically only return row if there is a value in either firstvalue or second value.
At the moment i have;
$conditions = array(
"NOT" => array(
'firstvalue' => ''
)
);
This works fine for the first value, but if i try and add anything to it, it still only returns the results for the firstvalue
Two ways to go about this:
$conditions = array(
'OR' => array(
array('NOT' => array('firstvalue' => '')),
array('NOT' => array('secondvalue' => ''))
)
);
Or, take advantage of the fact that "NOT a OR NOT b" == "NOT (a AND b)":
$conditions = array(
'NOT' => array(
array('firstvalue' => ''),
array('secondvalue' => '')
)
);
To add multiple fields in OR condition, you have to write the fields in array.
Like,
$conditions = array(
"OR" => array(
"NOT"=>array('firstvalue' => ''),
array('secondvalue'='')
)
)
);
I hope it will work for you.
I have the following if query.
if(($version[0] == 1)||($version[1] > 6)||($version[2]>=14 && $version[1] == 6))
{
echo "true";
}
Now I want this query translated to a mongodb query.
In the mongo collection I have the following entry:
{
"main_version" : 0,
"sub_version" : 6,
"build_version" : 14,
"module" : "Co2",
"active" : 1
}
When I try the following query I get a mongo error.
$where = array('$or' => array(
array('main_version'=> (int)$version[0]),
array('sub_version'=> array('$gt'=> (int)$version[1])),
array('$and' => array('build_version' => array('$gte'=> (int)$version[2])),
array('sub_version' => (int)$version[1]),
),
array('active'=> 1),
),
);
Error: Can't canonicalize query: BadValue and needs an array'
Does anybody know how to solve this query?
Well, worked in php 6 years back but I think your $and operator should accept array of queries,
something like array('$and' => array(array('build_version' => array('$gte'=> (int)$version[2])), array('sub_version' => (int)$version[1]))
'and' accepts array as you are doing for 'or' opeartor, here you are passing the query instead of array.
Yes, the extra array was the sollution. tx
I currently have:
$subQuery = $dbo->buildStatement(
array(
'fields' => array(
"CASE
WHEN
Application.program_type_id = 3
AND Application.program_type_id IS NOT NULL
THEN {$keys['program_type_id_program_type_id']}
ELSE 0
END as program_type_score,
CASE
WHEN
Application.priority_subject_area_id = 1
AND Application.priority_subject_area_id IS NOT NULL
THEN {$keys['priority_subject_area_id_priority_subject_area_id']}
ELSE 0
END as priority_subject_area_priority_subject_area_score,
User.*"
),
'table' => $dbo->fullTableName($this),
'alias' => 'User',
'limit' => null,
'offset' => null,
'joins' => $joins,
'conditions' => array(
'Application.state' => 'accepted',
'Role.role' => 'mentor'
),
'order' => null,
'group' => null
),
$this->User
);
I need to change the case statements from this:
CASE
WHEN
Application.program_type_id = 3
AND Application.program_type_id IS NOT NULL
THEN {$keys['program_type_id_program_type_id']}
ELSE 0
END as program_type_score
to this:
CASE
WHEN
Application.program_type_id = $user['User']['value']
AND Application.program_type_id IS NOT NULL
THEN {$keys['program_type_id_program_type_id']}
ELSE 0
END as program_type_score
How do I escape $user['User']['value']? Would Sanitize::escape() work, however, it is already deprecated.
I use the single quotes in php so the way I would do this would be:
'CASE
WHEN
Application.program_type_id = '.$user['User']['value'].'
AND Application.program_type_id IS NOT NULL
THEN {$keys['program_type_id_program_type_id']'}
ELSE 0
END as program_type_score'
enter code here
and you should be done.
One of the reasons I prefer the single quotes. sometimes a itsybitsy more work, but usualy no worry about escaping things. Atleast als long you don't mix HTML with Javascript using PHP variables. Then it gets always messy.
Hope that helps.
It seems that CakePHP does the escaping by itself on the find() method, as the docs say: http://book.cakephp.org/2.0/en/core-utility-libraries/sanitize.html#sql-escaping
I'm trying to get my head around the complex find conditions of CakePHP and have read the docs but am struggling with this one query.
SELECT field1,
field2
WHERE id = 123456
AND ((holding_date = Last_day(holding_date)
AND Month(holding_date) IN(3, 6, 9, 12))
OR (holding_date = '2013-09-15'))
To produce the above conditions what would my conditions array look like?
CakePHP conditions and sql expressions
While the conditions in the question are not that complex, they touch on a few points which mean they can be tricky to define correctly. Some of the things to know when defining cakephp conditions:
Conditions are defined as an array of key => value pairs, as such the same key cannot be defined twice on the same level
an array element which has a numeric key is interpreted as an sql expression
The default join mode is "AND" - it's not necessary to specify "AND" => ... in conditions
An OR conditions must have more than one elements. There's no error if it has only one but otherwise: OR what?
Bearing in mind the above notes, the conditions in the question can be expressed as:
$foo->find('all', array(
'fields' => array(
'field1',
'field2'
),
'conditions' => array(
'id' => 123456,
'OR' => array(
array(
'holding_date = LAST_DAY(holding_date)',
'MONTH(holding_date)' => array(3,6,9,12)
),
'holding_date' => '2013-09-15'
)
)
));
Which results in:
WHERE
`id` = 123456
AND
(
(
(holding_date = LAST_DAY(holding_date))
AND
(MONTH(holding_date) IN (3, 6, 9, 12)))
)
OR
(`holding_date` = '2013-09-15')
)
Note: whitespace is quite important =) I misread the question originally solely because of the inconsistent whitespace in the question's sql.
OK I have solved it:
$findParams['conditions'] = array(
'Account.client_id' => '12345',
'AND' => array(
'OR' => array(
'Holding.holding_date' => '2013-09-15',
'AND' => array(
'Holding.holding_date = LAST_DAY(Holding.holding_date)',
'MONTH(Holding.holding_date)' => array(3,6,9,12)
)
)
)
);
Try this:
$params['conditions'] = array(
'`id`' => 123456,
'AND' => array(
'`holding_date`' => 'LAST_DAY(`holding_date`)',
'AND' => array(
'MONTH(holding_date)' => array(3, 6, 9, 12),
'OR' => array(`holding_date` => '2013-09-15')
)
)
);
how do I build a find() query in cakePHP using these conditions:
Find where
MyModel.x = 1 and MyModel.y = 2 OR
MyModel.x = 1 and MyModel.y value does not exist (or is equal to empty string)
Can somebody tell me how I can go about building such find query?
I'm gonna give you some pointers, but you need to try to do this as it's very basic and it's always good to practice.
A basic find in cake is in the form of
$this->ModelName->find('all');
This in its default form does a SELECT * from model_names (convention is to have singular ModelName for plural table name - model_names)
To add conditions:
$this->ModelName->find('all', array('conditions' => array('ModelName.x' => 1));
To add AND conditions
$this->ModelName->find('all', array('conditions' => array(
'ModelName.x' => 1, 'ModelName.y' => 2
));
To add OR conditions
$this->ModelName->find('all', array('conditions' => array(
'OR' => array(
'ModelName.x' => 1, 'ModelName.y' => 2
)
));
To combine both
$this->ModelName->find('all', array('conditions' => array(
'ModelName.y is not' => null,
'OR' => array(
'ModelName.x' => 1, 'ModelName.y' => 2
)
));
// where y is not null and (x = 1 or y = 2)
http://book.cakephp.org/1.3/view/1030/Complex-Find-Conditions
(btw I'm sure there will be users giving you the exact answers, so just take my answer for your reference :) )
$this->MyModel->find('all', array('conditions' => array(
'OR' => array(
array(
'MyModel.x' => 1,
'MyModel.y' => 1
),
array(
'MyModle.x' => 1,
'OR' => array(
array('MyModel.y' => NULL),
array('MyModel.y' => '')
)
)
)
)));