Kohana 3 ORM: Getting most repeated values, ranked, and inserting into new object / array -
so, in series of kohana 3 orm questions :)
i have, essentially, pivot table, called connections
. connections
table connects song
keyword
. that's great , working (thanks last 2 questions!)
i want output connected songs keyword. so, somehow query connections
table , output object (with arbitrarily limited number of iterations $n
) ranks songs number of times have been connected, ie. number of times particular song_id
appears particular keyword_id
.
i have literally no idea how achieve this, without querying every single row (!!!) , counting individual results in array.... there must more elegant way achieve this?
i believe more of sql question. using db query builder:
db::select('songs.*')->select(array('count("keywords.id")', 'nconnections')) ->from('songs') ->join('connections', 'left')->on('connections.song_id', '=', 'songs.id') ->join('keywords', 'left')->on('connections.keyword_id', '=', 'keywords.id') ->group_by('songs.id') ->order_by('nconnections') ->as_object('model_song') ->execute();
or in sql
select `songs`.*, count(`keywords`.`id`) `nconnections` songs left join `connections` on `connections`.`song_id` = `songs`.`id` left join `keywords` on `connections`.`keyword_id` = `keywords`.`id` group `songs`.`id` order `nconnections`
should return result want.
you'll want have accessible property called nconnections
in song model. simplest way add public member don't tamper orm's inner workings.
i'm assuming you're using model called 'song', linked 'songs' table, 'keyword' model linked 'keywords' table , in 'connections' table foreign keys 'song_id' , 'keyword_id' each model respectively.
Comments
Post a Comment