mysql - CakePHP sub query SQL in HABTM relation -
I want to match the related products to the tags and sort order the most.
The relationship between the HABT model product and the tag
in the class product AppModel {//. Var is $ and bellongtaomany = array ("tag"); // ..}
and in contrast to the tag model. Includes - The table name is "products_tags". Examples of products $ $ include products [0] ['tags'] = array ('touch')
Ex.sample for
, 'Phone', '3g', 'apples'); // iPhone $ products [1] ['Tag'] = array ('phone', '3G', 'BB'); //bb $ products [2] ['tag'] = array ('touch', '3g', 'apple'); // ipad $ products [3] ['tag'] = array ('3G', 'air card'); // 3G Air Card
In this sample, the highest priority relates to the iPhone sort.
- The iPad (found in 3 tags)
- Air Force (only 1 match)
How to get a sub-query by using the cake method model:
ID as SELECT DISTINCT (product_id), leave the same tag count as / / sub-query * / select COUNT (*) tag tags as Product_tags on ProductTag (Tag .id = ProductTag.tag_id) WHERE product_id = ID and tag name ('touch', 'phone', '3g', 'apples')) Priority / * This is what tags as tags from / Tag as add (tag id = ProductTag.tag_id) tag. Order IN ('Touch', 'Phone', '3G', 'Apple') Priority Order by DESC
What exactly do I need to return the SQL query but I want CakePHP AppModel-> find () method can not find the way to parse the parameter.
If this SQL result does not use endorsement (assuming that the related product in the normal webpage 3-5 entries), why do not you use this complex SQL?
Then use the $ -> query ($ sql)
The sample code will be:
Create function related product () Class Product AppModel {//. Increases Var $ ANDBelongsToMany = array ("tag"); // .. function related product ($ parameter) {$ sql = "select ..... ou .... $ parameter ..."; Return $ $ - $ ($ sql) query; }}
Then you can use it in the controller by
$ this-> product-> related products ($ some_permatrums);
Comments
Post a Comment