Contributors have songs and songs have contributors. I want to be able to sort by the number of songs that a contributor has.
In my Controller:
public $paginate = array(
'fields' => array(
'Contributor.id',
'Contributor.name',
'COUNT(DISTINCT ContributorsSong.song_id) AS Contributor__TotalSongs',
),
'joins' => array(
array(
'alias' => 'ContributorsSong',
'table' => 'contributors_songs',
'type' => 'LEFT',
'conditions' => 'ContributorsSong.contributor_id = Contributor.id'
),
array(
'alias' => 'Song',
'table' => 'songs',
'type' => 'LEFT',
'conditions' => 'ContributorsSong.song_id = Song.id'
)
),
'group' => array('ContributorsSong.contributor_id')
);
And in my index method.
$this->Contributor->recursive = 0;
$this->Paginator->settings = $this->paginate;
$this->Contributor->virtualFields['TotalSongs'] = 0;
$items = $this->paginate();
echo '<pre>';print_r($items);echo '</pre>';
I'm trying to sort by the number of songs by using a virtual field, so when I go to
localhost/site/contributors/index/sort:TotalSongs/
I get this error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'order clause'
SQL Query: SELECT
Contributor.id,Contributor.name, COUNT(DISTINCT ContributorsSong.song_id) AS Contributor__TotalSongs FROMdb_songs2.contributorsASContributorLEFT JOINdb_songs2.contributors_songsASContributorsSongON (ContributorsSong.contributor_id=Contributor.id) LEFT JOINdb_songs2.songsASSongON (ContributorsSong.song_id=Song.id) WHERE 1 = 1 GROUP BYContributorsSong.contributor_idORDER BY (0) desc LIMIT 18
I thought that TotalSongs would get turned into Contributor__TotalSongs in the query but it gets turned into 0. What is going on here? Thanks.