I'm trying to figure out the best way to model my HABTM search. So far
i'm building upon http://bakery.cakephp.org/articles/view/habtm-searching
which is a great start.
At one point we're using this to query the database for each HABTM
relationship that exists:
$search_model->find('all', array( 'fields' => 'DISTINCT '. $id_field,
'conditions' => $condition, 'recursive' => -1, 'callbacks' => false //
because otherwise this `beforeFind` would be called again));
Which results in an sql query like this for each relationship:
SELECT DISTINCT `ModelSearch`.`model_id` FROM `models_searches` AS
`ModelSearch` WHERE `ModelSearch`.`search_id` = (X)
The result is that we query each relationship separately and then
compile it all to return as the result. So if the user check marks
choice 1, choice 2, and choice 3 (each choice being part of its own
model with its own HABTM relationship with the object) then we get all
objects that have EITHER choice 1 OR choice 2 OR choice 3.
My question is this: what is the best way to instead query objects
that have a relationship with choice 1 AND choice 2 AND choice 3.
The ID of each choice is available on the search result page as well
as the ID's of the relationships of the returned objects, so I could
use PHP to only display objects that match up. I'm hoping there is a
more elegant solution because not only does this require recursion of
1 so that I can see the ID's of the relationships but it also means
the database is being queried for many more objects than is necessary.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---