CakePHP Pagination Sort Data From Model

I currently have a query that results in the following set of records:

Array ( [Contestant] => Array ( [id] => 1 [name] => test [age] => [city] => atest [telephone] => [email] => test@test.com [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 paginator-> sorting to work with any data in it, except for "Contestant_votes", since it does not belong to the model that is currently in the array [0]

I tried to do this:

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

and this:

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

But that will not work. The conestants_votes field is generated by the following request:

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

So why is this not in the 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 a paginator so that I can sort it?

Thanks in advance,

Fabian Brenes

+3
source share
4

, CakePHP 1.3 .

Vote:

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

- , .

+1

Contestant_votes , 2 (..: Contestant__votes). Cake Contestant :

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

2 ?

 var $hasOne = array(

'' = > (  'className' = > '',  'fields' = > array ('SUM (Rating.score) AS Location__sum', 'AVG (Rating.score) AS Location__avg', 'COUNT (Rating.score) AS Location__count'),  'group' = > 'Rating.location_id', )  );

, Location ['sum']

0

. , , .

. mysql, .

, . 1000, :

if (!empty($options['order']) && is_array($options['order'])) {
    // lots of code goes here
}

( ):

if (!empty($options['order']) && is_array($options['order'])) {
 $key = key($options['order']);
 $value = $options['order'][$key];

 $key = preg_replace('/[^a-zA-Z_.]/', '', $key);

 $options['order'] = array();
 $options['order'][$key] = $value;
}

, - SQL-, , URL-. , .

I also stumbled upon this link, it would seem to be a less hacky way to do this in cake 1.3 (the code above was written for 1.2.5)

0
source

Source: https://habr.com/ru/post/1720853/


All Articles