0

I have 5 tables: tournaments, rounds, videos, rounds_tournaments, rounds_videos

Relations:

Tournament hasMany Video
Video belongsTo a Tournament

Tournament HABTM Round
Round HABTM Tournament

Video HABTM Round
Round HABTM Video

What I want to achieve:

Now I only want the videos that belong to a specific Tournament and Round.

So when clicking on a Tournament I only want the rounds associated with the clicked Tournament. And when clicking on a Round I only want the get the videos associated with the clicked Round and the Tournament.

I've tried everything but can't get it to work. I've played with several views as well (the index, view actions of Round/Tournament/Video). I'm not sure where to put the the JOIN statement, but I think it's inside the controller.

The Problem I've got atm is that when clicking on a Round it gives me the videos of that Round but for all the Tournaments and not for one Tournament.

Also looked here:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables

CakePHP find HABTM

Round Model:

public $hasAndBelongsToMany = array(
    'Tournament' => array(
        'className' => 'Tournament',
        'joinTable' => 'rounds_tournaments',
        'foreignKey' => 'round_id',
        'associationForeignKey' => 'tournament_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    ),
    'Video' => array(
        'className' => 'Video',
        'joinTable' => 'rounds_videos',
        'foreignKey' => 'round_id',
        'associationForeignKey' => 'video_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    )
);

Video Model:

public $belongsTo = array(
    'Tournament' => array(
        'className' => 'Tournament',
        'foreignKey' => 'tournament_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),

public $hasAndBelongsToMany = array(
    'Round' => array(
        'className' => 'Round',
        'joinTable' => 'rounds_videos',
        'foreignKey' => 'video_id',
        'associationForeignKey' => 'round_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    )
);

Tournament Model:

public $hasMany = array(
    'Video' => array(
        'className' => 'Video',
        'foreignKey' => 'tournament_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

public $hasAndBelongsToMany = array(
    'Round' => array(
        'className' => 'Round',
        'joinTable' => 'rounds_tournaments',
        'foreignKey' => 'tournament_id',
        'associationForeignKey' => 'round_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    ),
);

RoundsVideo model:

public $belongsTo = array(
    'Round' => array(
        'className' => 'Round',
        'foreignKey' => 'round_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Video' => array(
        'className' => 'Video',
        'foreignKey' => 'video_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

RoundsTournament model:

public $belongsTo = array(
    'Round' => array(
        'className' => 'Round',
        'foreignKey' => 'round_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Tournament' => array(
        'className' => 'Tournament',
        'foreignKey' => 'tournament_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

What I have tried:

index action of VideosController

$this->Video->recursive = -1;
    $options['joins'] = array(
        array('table' => 'rounds_videos',
            'alias' => 'RoundsVideo',
            'type' => 'inner',
            'conditions' => array(
                'Video.video_id = RoundsVideo.video_id'
            )
        ),
        array('table' => 'rounds',
            'alias' => 'Round',
            'type' => 'inner',
            'conditions' => array(
                'RoundsVideo.round_id = Round.round_id'
            )
        )
    );

    $this->set('videos', $this->Paginator->paginate());

index action of TournamentsController

$this->Tournament->recursive = -1;
    $options['joins'] = array(
        array('table' => 'rounds_tournaments',
            'alias' => 'RoundsTournament',
            'type' => 'inner',
            'conditions' => array(
                'Tournament.tournament_id = RoundsTournament.tournament_id'
            )
        ),
        array('table' => 'rounds',
            'alias' => 'Round',
            'type' => 'inner',
            'conditions' => array(
                'RoundsTournament.round_id = Round.round_id'
            )
        )
    );
    $this->set('tournaments', $this->Paginator->paginate());
Community
  • 1
  • 1
Gilko
  • 2,280
  • 8
  • 36
  • 45
  • You need to add what you have tried that is not working. For example, show the `find` statements, its results vs your expected results. As an aside: (1) You should not need to perform JOINs because you are creating the associations. (2) If you are making HABTM associations, it is not necessary to make models based on the JOIN tables. – AgRizzo Oct 19 '15 at 18:54
  • Don't set the recursive value (yet.) Just try something like `$this->Tournament->find('all', array('conditions' => array('Tournament.id' => '1')));` and dump/ examine the results. Does it have the related table information? Then use the [containable behavior](http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html) to limit the related tables' information. – AgRizzo Oct 19 '15 at 22:53
  • I get the Tournament with id 1 and also all the videos and rounds related to that Tournament. But don't get data from the HABTM tables. – Gilko Oct 20 '15 at 08:41

0 Answers0