tags:

views:

453

answers:

2

Hello, i would like to paginate a list of games, where the sport is a chosen sport. the relatition is as followed: Game BelongsTo Competition BelongsTo Team BelongsTo sport What i would like to do is show all games where the teams sport_id = 1. The following doesn't work:

        $this->paginate = array('limit' => 30, 'page' => 1, 
    'conditions' => array('Competition.Team.sport_id' => '1'),
        'contain' => array('Competition', 'Competition.Team',
        'Gamefield', 'Changingroom', 'ChangingroomAway', 'Gametype'),
    'order'=>array('game_date'=>'asc'),         
    );  

Can anyone help me with this one?

+2  A: 

I've found my solution:

    var $paginate=array(
    'Game'=>
    array(
        'joins'=>array(
            array('table'=>'competitions',
                  'alias'=>'Competition2',
                  'type'=>'left',
                  'conditions'=>array('Game.competition_id=Competition2.competition_id')
                  ),
            array('table'=>'teams',
                  'alias'=>'Team2',
                  'type'=>'left',
                  'conditions'=>array('Competition2.team_id=Team2.team_id')
                  ),
            ),
        'order'=>array('game_date'=>'asc'),
        'contains'=>array('Competition2'=>array('Team2'))
                    ));
function index() {
    $datum = date('Y-m-d H:m');
    $this->Game->recursive = 0;
    $scope=array('OR' => array(array('Team2.sport_id' => 2), array('Team2.sport_id' =>3)), 'Game.game_date >' => $datum);
//       Configure::write('debug',2);
    $this->set('games', $this->paginate(null,$scope));
}

Thanks to TehThreag for helping me

Michael
Just out of curiousity, what kind of performance degradation are you seeing here? If your tables are big, performing 2 joins on them could be really, really, really slow. Although I've never used it on pagination, the Containable behavior might be optimized for these joins; after all, a many-to-many join can be very expensive in the database layer.
Travis Leleu
this is a good example of ad-hoc-joins being used in production. A lot of people seem to be looking for how to use joins instead of multiple queries. Here is a perfect show and tell for them.
Abba Bryant
Travis, this isn't degrating the performance, this will only produce one query instead of with containable you could get multiple queries. If you look at the resulting query, you see that they both perform the same query, except for two extra joins. With the contains, you could get extra queries after this query.
Michael
A: 

Containable doesn't create any joins unless the relation would have been joined anyways, despite using Containable.

This means that for habtm, if you want a join you have to do as Michael did and specify them. Also, doing a couple of joins should be faster with logical indexes than doing a habtm with Containable anyways, as the results from a contain for the same data as above would require an in ( id1,id2,...id# ) condition and a number of queries from there to fetch the individual related records.

The joins solution gets the data back in one db query.

Abba Bryant