On 7/17/07, Grapetonic <[EMAIL PROTECTED]> wrote: > > > Now how can I, on a simple magic CakePHP way, also get all tags > related to the corresponding posts? I can probably get a foreach loop > to go thru all the posts and do a $this->Post- > >find(array('Post.id'=>"LIKE $post[id]") to get the corresponding tags > but that seems pretty excessive.
This is what we have in the Tag model for Cheesecake function findRelatedTags($tags=array()){ $prefix = $this->tablePrefix; $tagsCount = count($tags); if($tagsCount > 0){ $tagsString = "'".implode("','",$tags)."'"; $query = "SELECT Tags.*, COUNT(pt.photo_id) AS count FROM {$prefix}photos_tags pt, {$prefix}tags Tags WHERE pt.photo_id IN ( SELECT pt.photo_id FROM {$prefix}photos_tags pt, {$prefix}tags Tags WHERE pt.tag_id = Tags.id AND Tags.tag IN ({$tagsString}) GROUP BY pt.photo_id HAVING COUNT(pt.photo_id )={$tagsCount}) AND Tags.tag NOT IN ({$tagsString}) AND Tags.id = pt.tag_id GROUP BY pt.tag_id ORDER BY count DESC"; $relatedTags = $this->query($query); return $relatedTags; } You can of course make it more generic but that was not needed in our case HTH T -- ============================================================= Cheesecake-Photoblog: http://cheesecake-photoblog.org PHP for E-Biz: http://sanisoft.com ============================================================= --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---