This function is working on OR condition. I need this to work with AND condition. Any help:
$ands = array();
foreach ($array_training_id as $id) {
$ands[
Why is your $array_project_id a string in the first place (besides the naming conflict^^)? It should be an array of ids and cake will automatically be able to work with it out of the box.
Well, anyway:
$ids = explode(',', $array_project_id);
// now its a clean array
$users = $this->Training->find('all', array('conditions' =>
array('Training.Project_id' => $ids)
));
After your comments I still don't see the value in using AND here. But what the heck, here you go:
$ands = array();
foreach ($ids as $id) {
$ands[] = array('Training.Project_id' => $id);
}
$conditions = array('AND' => $ands);
$users = $this->Training->find('all', array('conditions' => $conditions));
Voila. It will still never work, though.
It seems you are using some HABTM relation (you should have provided this vital piece of information in the beginning!). Then you need to query the intermediate model/relation in order to have two belongsTo-relations (= left joins) here.
What you REALLY need (again: why did you just post the relevant relation and the information about Training_users now?) is to query the intermediate table "Training_users" with one contain in the belongsTo direction (left joins to users). And the most important fact: using OR instead of AND. This will get the results you want.
The easiest approach would be, to create a habtm-relation.
Then you can do:
$users = $this->User->Training->find('all', array('conditions' => array('User.id =' => $ids)));
This finds all where User.id = $id
With your AND-condition, it would be:
$users = $this->User->Training->find('all', array('conditions' =>
'AND' => array('User.id =' => $ids['n'], 'User.id =' => $ids['n'])
));