Take a look at this: https://github.com/CakeDC/search It'll do this for you
very nicely.
Jeremy Burns
Class Outfit
http://www.classoutfit.com
On 2 Jan 2012, at 18:10:23, Dwayne Hanekamp wrote:
> Hello all,
>
> I've been struggling on this for the last two days. I'm creating a
> webshop on which people should be able to filter products on multiple
> tags. Database structure is the following:
>
> Tags => Id | Name
> Products => Id | Category_id | Name | etc...
> Tags_Products | Id | Product_id | Tag_id
> Categories | Id | parent_id | name
>
> The user can first select a category and each category has
> subcategories which have products which have their tags. So after
> they've selected a category or subcategory they can filter down using
> the tags of the products in the categories.
>
> For the find query i made next options array:
> Array ( [fields] => Array ( [0] => DISTINCT Product.id [1] =>
> Product.* ) [conditions] => Array ( [OR] => Array
> ( [Product.Category_id] => 1 [Category.parent_id] => 1 ) [AND] =>
> Array ( [AND] => Array ( [FilterTag.name] => Array ( [0] => smartphone
> [1] => apple ) ) ) ) )
>
> The 'FilterTag' is a fake model made in the function beneath:
> 'hasOne' => array(
> 'ProductsTag',
> 'FilterTag' => array(
> 'className' => 'Tag',
> 'foreignKey' => false,
> 'conditions' => array('FilterTag.id =
> ProductsTag.tag_id'),
> 'unique' => true,
> )
>
> Now the problem is that i want it to find products that have both tags
> in stead of one of the tags. It currently does the next query:
>
> SELECT DISTINCT `Product`.`id`, `Product`.*, `Product`.`id` FROM
> `products` AS `Product` LEFT JOIN `categories` AS `Category` ON
> (`Product`.`category_id` = `Category`.`id`) LEFT JOIN `products_tags`
> AS `ProductsTag` ON (`ProductsTag`.`product_id` = `Product`.`id`) LEFT
> JOIN `tags` AS `FilterTag` ON (`FilterTag`.`id` =
> `ProductsTag`.`tag_id`) WHERE ((`Product`.`Category_id` = '1') OR
> (`Category`.`parent_id` = 1)) AND `FilterTag`.`name` IN ('smartphone',
> 'apple')
>
> So it looks for products which have one of the tags. But i want it to
> search for products that have both of the tags.
>
> I hope you guys can help me, Thanks in advance!
>
> Dwayne
>
> --
> Our newest site for the community: CakePHP Video Tutorials
> http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help
> others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> cake-php+unsubscr...@googlegroups.com For more options, visit this group at
> http://groups.google.com/group/cake-php
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at
http://groups.google.com/group/cake-php