I've been pulling my hair over this puzzle for two days now. I have
this setup (not actual models - these are changed for clarity):
Post belongsTo User
Post hasAndBelongsToMany Category
Post hasAndBelongsToMany Tag
I'm doing a search via a filter form and I need a paginated result
set. I'm familiar with alternative ways of modeling the Habtm
associations - a) using a dummy habtm model; b) using an implied
(auto) or an explicitly designated 'with' association model - that
could be used to filter by the foreign habtm model, but the problem is
that all articles or discussions in the groups I've read are dealing
with a single HABTM association on which the filtering is done.
In my case I couldn't paginate or findAll() via the habtm model,
because I have search conditions in other models associated with
'Post' - e.g. I need all posts belonging to a specific user,
associated with a specific category_id and with a specific tag_id.
I'm not posting all the code in my controller/action - just the
relevant statements.
The following works fine:
$filterConditions = array(
'Post.active' => 1,
'Post.user_id' => $user_id,
'CategoryPost.category_id' => $category_id,
'PostTag.tag_id' => $tag_id,
);
$posts = $this->paginate('Post', $filterConditions);
Now, I have two valid requirements which are similar to the above and
which I'm not able to come up with solutions to:
1) I need the Posts associated to either category_id 'X' or
category_id 'Y':
$filterConditions = array(
'Post.active' => 1,
'Post.user_id' => $user_id,
'CategoryPost.category_id' => array($cat1_id, $cat2_id),
'PostTag.tag_id' => $tag_id,
);
$posts = $this->paginate('Post', $filterConditions);
The issue with this scenario is that I'm getting duplicate records if
a Post has been associated with both $cat1_id and $cat2_id. I found
several discussions mentioning a way of passing a $fields array() to
findAll() and specifying 'DISTINCT PrimaryModel.id' in it. Ok, but I
have no way of passing a similar $fields array to
Controller::paginate(). Have I missed anything or is this requirement
not achievable at all without custom queries.
2) To complicate things even further I need to be able to query for
Posts that are associated to both categories $cat1_id AND $cat2_id. I
am not even able to come up with the correct syntax for specifying the
conditions. It just doesn't feel right to use something like:
$filterConditions = array(
'Post.active' => 1,
'Post.user_id' => $user_id,
'CategoryPost.category_id' => $cat1_id,
'CategoryPost.category_id' => $cat2_id,
'PostTag.tag_id' => $tag_id,
);
I'd be extremely thankful for any hints on the right solution to these
issues!
Regards,
Nasko
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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
-~----------~----~----~----~------~----~------~--~---