Let’s say I have a simple database and I’m trying to find all users who speak both English and Spanish and/or English and French (but not Spanish and French). I have a users table with a handful of records set up like this:
{
"username": "jdoe",
"languages": ["english", "spanish", "french"]
}
I get the expected results when I search like this:
db.find({ "languages" : { $all:['english', 'spanish'], $all:['english', 'french'] } })
But I’m not sure how to do it with Cake. I can match a single language pair like so:
$this->User->find('all', array('conditions' => 'User.languages' => array('$all' => array('english', 'spanish'))));
But I’m stuck on how to search for multiple pairs; an array of arrays doesn’t seem to do it.
This is a project someone else started, so I’m not particularly familiar with the nuances of CakePHP or MongoDB; I have a feeling I’m overlooking something obvious. Any ideas?
Your shell query example may have returned the expected results for your test data, but it’s technically incorrect since you’re clobbering the
$allproperty in the query object. Using the top-level$oroperator, the correct query would be:This translates to CakePHP as:
I tested this using the following fixture data (sans
_id‘s) in a CakePHP app:John, Fred and Luke are returned.