if want create category , able link products tags can so:
- create category , product tables.
- create tags table tags like: ruby, earrings, white-gold
- create
category_tags
, product_tags
table map them - set category , product hasandbelongstomany tags
- set tags hasandbelongstomany products , hasandbelongstomany categories
now have 2 products 1 tags: ruby , earrings , tags: ruby , bracelet
say want create ruby earrings category.
i add ruby , earrings tags category. under normal habtm model associations both products returned because though 1 has earrings
tag both have ruby
tag.
how can make match products have of same tags category (products can have more tags must have tags corresponding category has) in order returned?
also, taking further, how add -tags
category products must not have these tags returned?
the script below solved issue generating query so:
php
$data = $this->designer->find( 'first', array( 'conditions' => array( 'designer.slug' => $name, 'designer.available' => 1 ) ) ); $inc_tag_ids = array(); $exc_tag_ids = array(); foreach($data["tag"] $tag) { if( $tag['designerstag']['include'] ) { $inc_tag_ids[] = $tag['id']; } else { $exc_tag_ids[] = $tag['id']; } } $ins = ' '; if( count($inc_tag_ids) ) { $inc_tag_id_str = '"' . implode('","',$inc_tag_ids) . '"'; $ins .= 'and tags.id in ('.$inc_tag_id_str.')'; } if( count($exc_tag_ids) ) { $exc_tag_id_str = '"' . implode('","',$exc_tag_ids) . '"'; $ins .= 'and products.id not in ( select products.id products, products_tags, tags products.id = products_tags.product_id , tags.id = products_tags.tag_id , tags.id in ('.$exc_tag_id_str.') )'; } $prod_qry = ' select *, count(distinct tags.name) uniques products, products_tags, tags products.id = products_tags.product_id , tags.id = products_tags.tag_id '.$ins.' group products.id having uniques = '.count($inc_tag_ids).' '; echo $prod_qry; $data["matching_products"] = $this->designer->tag->query($prod_qry);
sql
select * , count( distinct tags.name ) uniques products, products_tags, tags products.id = products_tags.product_id , tags.id = products_tags.tag_id , tags.id in ( "8" ) , products.id not in ( select products.id products, products_tags, tags products.id = products_tags.product_id , tags.id = products_tags.tag_id , tags.id in ( "7" ) ) group products.id having uniques =1
however feel not way cakephp inteded treated, think maybe should handled in model not in controller. not sure how that.
Comments
Post a Comment