On Dec 15, 10:08 am, kai <[email protected]> wrote:
> I'm trying to figure out the best way to model my HABTM search. So far
> i'm building uponhttp://bakery.cakephp.org/articles/view/habtm-searching
> which is a great start.

Why not use the examples in the book as your start:
http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM

It has examples of how to perform habtm searches, and if the
information is lacking I would encourage you (or anyone) to add to the
book what you feel is missing/would help.

The bakery article is useful, but also implies if it's the first thing
you're reading on the subject that a (simple/standard) habtm search
requires complex logic to achive (which quite simply, it doesn't).

>
> 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.

See
http://groups.google.com/group/cake-php/browse_thread/thread/8cbf01f7a9acda57/9e84f7aacbf3cd85

>
> 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 can do what you're trying to do in 1 query (2 if you include the
count to the get the paginate total).

hth

AD

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to