Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp Pagination Sort data out of model

I currently have a query that results in the following recordset:

Array ( [Contestant] => Array ( [id] => 1 [name] => test [age] => [city] => atest [telephone] => [email] => [email protected] [why_model_house] => a [highschool] => [photo] => 5329_119145013633_512383633_2487923_7196193_n0.jpg [active] => 1 ) [0] => Array ( [Contestant_votes] => 4 ) [Vote] => Array ( [id] => 1 ) )

I can get the paginator->sort to work with every data in it except the "Contestant_votes" since it doesn't belong in a model it's currently in the Array[0]

I tried doing this :

        <th><?php echo $paginator->sort('Votes', '0.Contestant_votes'); ?></th> 

and this:

        <th><?php echo $paginator->sort('Votes', 'Contestant_votes'); ?></th> 

But it doesn't work. The conestants_votes field is generated by the following query:

'Contestant.*, count(Vote.contestant_id) as Contestant_votes'

So that's why it's not in a model. Is there a way to trick cakephp into thinking that Contestant_votes is part of the Contestant model or a way to add it to the paginator so I can sort it?

Thanks in advance,

Fabian Brenes

like image 247
Fabian Brenes Avatar asked Jan 23 '26 17:01

Fabian Brenes


1 Answers

This is exactly what CakePHP 1.3 virtual fields are meant for.

In your case, put following in your Vote model:

var $virtualFields = array(
    'Contestant_votes' => 'COUNT(Vote.contestant_id)'
);

Beware though - behaviour of this virtual field will change if you add grouping to your query.

like image 94
lxa Avatar answered Jan 26 '26 09:01

lxa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!