How to add multiple conditions for a single column? - cakephp

I have many sentences that I need filter for a same column:
'conditions' => array('Zona.nombre LIKE' => $buscar,
'Zona.nombre LIKE' => 'CUPONATIC%',
'Zona.nombre LIKE' => 'GROUPON%'
),

your question is not very clear but I suppose the problem is that you use multiple time the same array key
You don't even mention the cakephp version but it seems cake2
If I remember well the workaround for cake2 is putting every condition in a different array
'conditions' => array(
array('Zona.nombre LIKE' => $buscar),
array('Zona.nombre LIKE' => 'CUPONATIC%'),
array('Zona.nombre LIKE' => 'GROUPON%')
),
edit: of course this way you'll have the 3 conditions joined in AND.
It seems more logical to put them in OR so
'conditions' => array(
'OR' => array(
array('Zona.nombre LIKE' => $buscar),
array('Zona.nombre LIKE' => 'CUPONATIC%'),
array('Zona.nombre LIKE' => 'GROUPON%')
)
),

Related

How to apply && between 3 $conditions[] ? in cakephp

In Cakephp how can i add apply 'AND' between these lines , $conditions[]= codition1 && codition2 && condition3;
$conditions[]= array('Flight.from LIKE' => "%".$this->request->data['Flight']['from']."%");
$conditions[]= array('Flight.to LIKE' => "%".$this->request->data['Flight']['to']."%");
$conditions[]= array('Flight.date LIKE' => "%".$this->request->data['Flight']['date']."%");
You can simplify your query by using:
$conditions = array(
'Flight.from LIKE' => "%".$this->request->data['Flight']['from']."%",
'Flight.to LIKE' => "%".$this->request->data['Flight']['to']."%",
'Flight.date LIKE' => "%".$this->request->data['Flight']['date']."%"
);
You don't actually need the arrays around each field when the conditions are on different fields. Plus CakePHP will automatically use AND between your conditions.
$conditions = array(
'AND' => array(
'Flight.from LIKE' => "%".$this->request->data['Flight']['from']."%",
'Flight.to LIKE' => "%".$this->request->data['Flight']['to']."%",
'Flight.date LIKE' => "%".$this->request->data['Flight']['date']."%"
)
);
Official docs and examples here.

How to mix AND OR operators in find conditions CakePHP

I want to write this query in CakePHP:
SELECT * FROM pm_management.chk_master_xs_tasks
where (eq_model = 'D11 R' OR eq_model = 'TODOS') AND (pm_type = 'XD' OR pm_type = 'XS');
I have tried severals ways like this:
$this->ChkMasterXsTask->find('all', array(
'conditions' => array('AND' => array(
array('OR' => array('ChkMasterXsTask.eq_model' => $eq_model,
'ChkMasterXsTask.eq_model' => 'TODOS')),
array('OR' => array('ChkMasterXsTask.pm_type' => $pm_type,
'ChkMasterXsTask.pm_type' => 'XS'))
)
),
));
But the result is not the expected. Also I saw related questions but doesn't work for me.
Thank you so much for your answers.
Your problem arises from this code (and this problem is repeated for eq_model)
array('ChkMasterXsTask.pm_type' => $pm_type,
'ChkMasterXsTask.pm_type' => 'XS')
which tries to assign 2 values to the same key of that associative array. You could rewrite your code to this:
array(array('ChkMasterXsTask.pm_type' => $pm_type),
array('ChkMasterXsTask.pm_type' => 'XS'))
Another option which improves readability, uses SQL's IN function. The equivalent SQL becomes
SELECT *
FROM pm_management.chk_master_xs_tasks
WHERE eq_model IN ('D11 R', 'TODOS')
AND pm_type IN ('XD', 'XS');
Which should make your find a little easier write:
$this->ChkMasterXsTask->find('all', array(
'conditions' => array(
'ChkMasterXsTask.eq_model' => array($eq_model, 'TODOS'),
'ChkMasterXsTask.pm_type' => array($pm_type, 'XS'))
));
just I see a error y your code, there is a comma, that shouldn't be there.
$this->ChkMasterXsTask->find(
'all', array(
'conditions' => array(
'AND' =>
array('OR' => array(
array('ChkMasterXsTask.eq_model' => $eq_model),
array('ChkMasterXsTask.eq_model' => 'TODOS')
),
array('OR' =>
array('ChkMasterXsTask.pm_type' => $pm_type),
array('ChkMasterXsTask.pm_type' => 'XS')
)
))
));

CakePHP 2 How to find last record with hasn't related model empty

I use find('first') with order by created field for return last record from table.
But I would like return last record if his related model isn't empty.
It is possible?
Edit:
I add code, but doesn't work:
$last_project = $this->Project->find('first', array(
'conditions' => array(
'COUNT(ProjectPhoto.id) >' => 0),
'joins' => array(
array(
'table' => 'project_photos',
'alias' => 'ProjectPhoto',
'type' => 'INNER',
'conditions' => array(
'Project.id = ProjectPhoto.project_id'))),
'order' => 'Project.created DESC'));

CakePHP adding array elements to an array

I think I have my dumber-than-usual head on today.
I am trying to parse text from a textbox in a form to generate an array that I can pass to the find method in cakePHP.
I have used php regular expressions to generate an array of the terms entered (including "phrases included in speech marks" as single item)
This may look like this:
array(
(int) 0 => 'test',
(int) 1 => 'this out',
(int) 2 => 'now')
In the cakePHP cookbook it says that I have to get an array that looks like this (using above example):
array(
array('ProjectDocumentSectionVariable.variable_description LIKE' => '%test%'),
array('ProjectDocumentSectionVariable.variable_description LIKE' => '%this out%'),
array('ProjectDocumentSectionVariable.variable_description LIKE' => '%now%'))
Try as I might, I end up with arrays that do NOT look like an array of arrays.
Typically I am ending up with this horrible looking thing:
array(
(int) 0 => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%test%'
),
(int) 1 => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%this out%'
),
(int) 2 => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%now%'
))
The code for the above is:
$i = 0;
foreach($matches[1] as $match) :
$searcharray['ProjectDocumentSectionVariable.variable_description LIKE'] = "%" . $match . "%";
array_push($array_full,$searcharray);
$i++;
endforeach;
THis must be a common requirement, so I am hoping one of you guys could put me on the straight an narrow...
Thanks
If you're trying to build a search query (not clear what your goal is), your conditions should all be in the same array within an OR or AND - something like this:
$searchResults = $this->find('all', array(
'conditions' => array(
'OR' => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%test%',
'ProjectDocumentSectionVariable.variable_description LIKE' => '%this out%',
'ProjectDocumentSectionVariable.variable_description LIKE' => '%now%'
)
)
));
If you want to build it out in a foreach() loop, you can do it something like this:
$conditions = array('OR' => array());
$matches = array('test', 'this out', 'now');
foreach($matches as $match) {
array_push($conditions['OR'], array('variable_description LIKE' => '%' . %match . '%'));
}
$searchResults = $this->find('all', array(
'conditions' => $conditions
));
its as simple as this:
$array= array();
foreach ( $colors as $c):
$uniques[] = $c;
endforeach;
not having the [] will treat it as a regular variable. This will append it to the array

cakephp unable to sort on afterfind virtual fields with paginate

I have a query that I am running through paginate. This query contains a model ("PaymentException") that has an afterfind method that tacks on a copy of the last "ExceptionWorkflowLog", and calls it "LastWorkflowLog".
The query being passed to paginate:
$this->paginate = array(
'fields' => array(
'PaymentException.*', 'Procedure.id', 'Procedure.cpt',
'Procedure.expected_amount', 'Procedure.allowed_amount', 'Procedure.difference_amount',
'Claim.id', 'Claim.number', 'Payer.abbr'
),
'limit' => 50,
'joins' => array(
array(
'table' => 'procedures',
'alias' => 'Procedure',
'conditions' => array('Procedure.id = PaymentException.procedure_id')
),
array(
'table' => 'claims',
'alias' => 'Claim',
'conditions' => array('Claim.id = Procedure.claim_id')
),
array(
'table' => 'payers',
'alias' => 'Payer',
'conditions' => array('Payer.id = Procedure.payer_id')
),
array(
'table' => 'groups',
'alias' => 'Groups',
'conditions' => array('Groups.id = Claim.group_id')
)
),
'conditions' => $conditions,
'contain' => array('ExceptionWorkflowLog')
);
The resulting array (from the query that combines both "PaymentException", "ExceptionWorkflowLog", and "LastWorkflowLog") looks like below:
0 =>
'PaymentException' => array(fields and values),
'ExceptionWorkflowLog' => array(of ExceptionWorkflowLogs),
'LastWorkflowLog' => array(fields and values of the last indexed ExceptionWorkflowLog)
1 => ...
ExceptionWorkflowLog is mapped to PaymentException by PaymentException.id. It's a many to one relationship (thus the array of results under the ExceptionWorkflowLog).
I would like to use paginate to sort on the "updated" field on either the last indexed ExceptionWorkflowLog or the LastWorkflowLog.
Is there a way to do this with paginate? Currently, if I set the table heading to point to "LastWorkflowLog.updated", the query returns false because the query doesn't know what "LastWorkflowLog" is.
Since this has a couple hundred views, I figured I'd come back and post what I did. CakePHP's handling of joins is absolutely terrible. I rewrote the query to not use joins, but use contains. That seems to have solved it. I feel dirty.

Resources