多个hasMany通过列别名

时间:2015-07-26 14:45:17

标签: mysql cakephp model-associations

我为垒球队制作了一个小应用程序。我想建立以下关联:

模型Game有两列away_idhome_id,这两列都是Team.id的别名。

Game模型中我有:

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Home' => array(
            'className' => 'Team',
            'foreignKey' => 'home_id', // also tried simply 'id'
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Away' => array(
            'className' => 'Team',
            'foreignKey' => 'away_id', // also tried simply 'id'
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

Team模型中我有:

public $virtualFields = ['home_id' => 'Team.id', 'away_id' => 'Team.id'];

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Game' => array(
                    'className' => 'Game',
                    'foreignKey' => 'home_id',
                    'dependent' => false,
                    'conditions' => '',
                    'fields' => '',
                    'order' => '',
                    'limit' => '',
                    'offset' => '',
                    'exclusive' => '',
                    'finderQuery' => '',
                    'counterQuery' => ''
                ),


// also tried only one 'Game' association with foreignKey => [home_id, away_id]
        'Game' => array(
                    'className' => 'Game',
                    'foreignKey' => 'away_id',
                    'dependent' => false,
                    'conditions' => '',
                    'fields' => '',
                    'order' => '',
                    'limit' => '',
                    'offset' => '',
                    'exclusive' => '',
                    'finderQuery' => '',
                    'counterQuery' => ''
                )
    );

收到以下错误:

注意:" Psychouts"是下面SQL中唯一不可解释的外观;它是数据库名称(以及我的团队名称,henh)。

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Team.id' in 'field list'

SQL查询:SELECT GameidGamewhenGamecancelledGame。{ {1}},fieldGamehome_idGameaway_idGamescore_us。{{1 },Gamescore_themHomeidHomecaptain_idHomenameHomewinsHomelossesHometiesHomescored,( Homeallowed)AS Team,(idHome__home_id)AS TeamidHome__away_idAwayidAwaycaptain_idAwaynameAwaywins,{ {1}}。AwaylossesAwaytiesAway,(scoredAway)AS { {1}},(allowedTeam)AS id FROM Away__home_idTeam AS id LEFT JOIN Away__away_idpsychouts AS games ON(Gamepsychouts = teamsHome)LEFT JOIN Gamehome_id AS Home ON(idpsychouts = teamsAway)WHERE 1 = 1 LIMIT 20

1 个答案:

答案 0 :(得分:0)

根据AD7six的评论,经过测试,似乎正确的解决方案是:

Team模型中,删除我建议的虚拟字段,然后按如下方式定义Game关联:

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Game' => array(
            'className' => 'Game',
            'foreignKey' => ['OR' => ['Game.home_id = Team.id', 'Game.away_id = Team.id']]
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
    );

Game模型中,像往常一样定义Team关联:

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Team' => array(
            'className' => 'Team',
            'foreignKey' => 'id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
    );