HABTM内部联接CakePHP 2.0

时间:2015-10-18 19:34:12

标签: cakephp join cakephp-2.0 inner-join has-and-belongs-to-many

我有5个表:tournamentsroundsvideosrounds_tournamentsrounds_videos

关系:

锦标赛 hasMany 视频
视频属于锦标赛

比赛 HABTM 比赛轮次 轮次 HABTM 锦标赛

视频 HABTM 圆形 Round HABTM 视频

我想要实现的目标:

现在我只想要属于特定锦标赛和回合的视频。

因此,当点击锦标赛时,我只想要与点击的锦标赛相关的轮次。点击圆形时我只想获得与点击的圆形和锦标赛相关的视频。

我已经尝试了一切但却无法让它发挥作用。我也玩了几个视图(索引,查看Round / Tournament / Video的动作)。我不知道在哪里放置JOIN语句,但我认为它在控制器内部。

我遇到的问题是,当点击圆形时,它会为我提供该轮的视频,但是对于所有锦标赛而不是一场锦标赛。

另见这里:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables

CakePHP find HABTM

圆形模型:

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' => '',
    )
);

视频模型:

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' => '',
    )
);

锦标赛模型:

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模型:

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

RoundsTournament模型:

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

我尝试了什么:

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());

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());

0 个答案:

没有答案