【问题标题】:Difficulty with Associations协会的困难
【发布时间】:2014-07-08 04:26:37
【问题描述】:

我目前有几个模型。关联如下:

  • 安排 hasMany Match
  • 事件有很多时间表
  • 事件有很多部门
  • TeamEvent hasMany Match(两场,客场和主场)
  • Team hasMany TeamEvent(将实际团队链接到部门)
  • Division hasMany TeamEvent

我正在尝试查找每个 TeamEvent 的所有匹配项。

$this->TeamEvent->find('all', array('conditions' => array('TeamEvent.team_id' => $this->Team->id)));

问题是所有比赛都被放入 ID 为 1 的 TeamEvent 中。

这是相关的 SQL:

SELECT `Match`.`id`, `Match`.`schedule_id`, `Match`.`event_id`, `Match`.`home_team_id`, `Match`.`away_team_id`, `Match`.`home_result`, `Match`.`away_result`, `Match`.`matchtime`, `Schedule`.`id`, `Schedule`.`week`, `Schedule`.`map_name`, `Schedule`.`start_date`, `Schedule`.`end_date`, `Schedule`.`event_id`, `HomeTeam`.`id`, `HomeTeam`.`division_id`, `HomeTeam`.`team_id`, `HomeTeam`.`active`, `HomeTeam`.`win`, `HomeTeam`.`loss`, `AwayTeam`.`id`, `AwayTeam`.`division_id`, `AwayTeam`.`team_id`, `AwayTeam`.`active`, `AwayTeam`.`win`, `AwayTeam`.`loss` FROM `openplay`.`matches` AS `Match` LEFT JOIN `openplay`.`schedules` AS `Schedule` ON (`Match`.`schedule_id` = `Schedule`.`id`) LEFT JOIN `openplay`.`team_events` AS `HomeTeam` ON (`Match`.`home_team_id` = `HomeTeam`.`id`) LEFT JOIN `openplay`.`team_events` AS `AwayTeam` ON (`Match`.`away_team_id` = `AwayTeam`.`id`) WHERE ((`HomeTeam`.`team_id` = 1) OR (`AwayTeam`.`team_id` = 1)) ORDER BY `Match`.`matchtime` desc LIMIT 1

如您所见,唯一的条件是 Away and Home TeamEvent。我怎样才能让它也检查时间表?

【问题讨论】:

    标签: cakephp associations models


    【解决方案1】:
    $this->Team->id
    

    它保存了指向 Team 模型的当前指针 - 我不建议使用它,除非你知道自己在做什么,我建议在你的函数调用中使用一个参数:

    public function something($team_id) {
        $matches = $this->TeamEvent->find('all', array('conditions' => array('TeamEvent.team_id' => $team_id)));
    //etc
    
    }
    

    【讨论】:

    • 感谢帮助,但是即使使用参数也不起作用。
    • 如果您使用控制器代码更新原始问题,它可能会有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多