I have a many-to-many database structure that matches "classifieds" to "categories" through "a classifieds_categories" join table.
This works for matching
any of the category ids (searchArray are the category ids) and gives results that either match ID 2 or ID 24.
$searchArray = array(2,24);
$search = array('CtcmCategories.id:IN' => $searchArray);
$classifieds = $modx->getCollectionGraph('CtcmClassifieds', '{"CtcmClassifiedsCategories":{"CtcmCategories":{"CtcmCategoryTypes":{}}}}', $search);
foreach ($classifieds as $classified) {
$output .= $classified->get('title').' ('.$classified->get('id').')
';
foreach ($classified->getMany('CtcmClassifiedsCategories') as $classifiedCategories) {
$output .= "A Classified with a catgory of " . $classifiedCategories->getOne('CtcmCategories')->get('label') . " and a type of ".$classifiedCategories->getOne('CtcmCategories')->getOne('CtcmCategoryTypes')->get('label')." was found.
";
}
}
Here is the problem:
I need to get only the records that match
all the categories - so results that have both a category ID 2 AND a category ID 24 in the join table.
This works in the MySQL database:
SELECT * FROM `modx_ctcm_classifieds`
INNER JOIN (
SELECT classified_id
FROM `modx_ctcm_classifieds_categories`
WHERE category_id IN (2,24)
GROUP BY classified_id
HAVING count(*) > 1
) valid ON `modx_ctcm_classifieds`.id = valid.classified_id
How do I get this into xPDO efficiently?? Does anyone have any pointers? I have a feeling that I'm missing something simple here...this shouldn't be that difficut!
Any help is greatly appreciated!!
Thomas
[ed. note: twincreekmedia last edited this post 14 years, 4 months ago.]