0

我真的在为 cakephp 中的表关联而苦苦挣扎,我想做的就是将两个表关联在一起,但是一个表有两个外键到另一个表。

出于某种原因,我无法在 home_id ... 的字段选择表单中显示任何信息。

请参阅下面的数据库图片和相关代码。

我对 cakephp 真的很陌生,但觉得如果我能做到这一点,这将非常有用 - 非常感谢任何帮助。

数据库模式

class SafcTeam extends AppModel {
    var $name = 'SafcTeam';
    var $displayField = 'name';
    var $validate = array(
            'name' => array(
                    'notempty' => array(
                            'rule' => array('notempty'),
                    ),
            ),
            'badge' => array(
                    'notempty' => array(
                            'rule' => array('notempty'),
                    ),
            ),
    );

    var $hasMany = array(
            'SafcTeam' => array(
                    'className' => 'SafcEvent',
                    'foreignKey' => 'home_id',
            )
    );

}


class SafcEvent extends AppModel {
    var $name = 'SafcEvent';
    var $displayField = 'id';
    var $validate = array(
            'safc_matchtype_id' => array(
                    'numeric' => array(
                            'rule' => array('numeric'),
                    ),
            ),
            'safc_league_id' => array(
                    'numeric' => array(
                            'rule' => array('numeric'),
                    ),
            ),
            'home_id' => array(
                    'numeric' => array(
                            'rule' => array('numeric'),
                    ),
            ),
            'away_id' => array(
                    'numeric' => array(
                            'rule' => array('numeric'),
                    ),
            ),
            'streamer_id' => array(
                    'numeric' => array(
                            'rule' => array('numeric'),
                    ),
            ),
            'safc_channel_id' => array(
                    'numeric' => array(
                            'rule' => array('numeric'),
                    ),
            ),
            'comments' => array(
                    'notempty' => array(
                            'rule' => array('notempty'),
                    ),
            ),
            'safc_profile_id' => array(
                    'numeric' => array(
                            'rule' => array('numeric'),
                    ),
            ),
            'safc_source_id' => array(
                    'numeric' => array(
                            'rule' => array('numeric'),
                    ),
            ),
            'event_info_url' => array(
                    'notempty' => array(
                            'rule' => array('notempty'),
                    ),
            ),
    );

    var $belongsTo = array(
            'SafcMatchtype' => array(
                    'className' => 'SafcMatchtype',
                    'foreignKey' => 'safc_matchtype_id'
            ),
            'SafcLeague' => array(
                    'className' => 'SafcLeague',
                    'foreignKey' => 'safc_league_id'
            ),
            'SafcChannel' => array(
                    'className' => 'SafcChannel',
                    'foreignKey' => 'safc_channel_id'
            ),
            'SafcProfile' => array(
                    'className' => 'SafcProfile',
                    'foreignKey' => 'safc_profile_id'
            ),
            'SafcSource' => array(
                    'className' => 'SafcSource',
                    'foreignKey' => 'safc_source_id'
            ),
            'SafcTeam' => array(
                    'className' => 'SafcTeam',
                    'foreignKey' => 'home_id'
            )
    );

}

4

2 回答 2

0

Cake 不支持开箱即用的复杂 FK,您可以像这样定义具有条件的关系

public $hasMany = array(
    'TheRelation' => array(
        'foreignKey' => false,
        'conditions' => array(
            ... conditions ...
        )
    )
)
于 2012-12-14T21:07:10.843 回答
0

您必须为 SafcTeam 模型的每个关系使用别名:

var $belongsTo = array(

//relationship for your home_id fk
        'SafcHomeTeam' => array(
                'className' => 'SafcTeam',
                'foreignKey' => 'home_id'
        ),

 //relationship for your away_id key
            'SafcAwayTeam' => array(
                    'className' => 'SafcTeam',
                    'foreignKey' => 'away_id'
            )
    );

以及相关的 safc 团队模型中:

var $hasMany = array(
        'SafcHomeTeam' => array(
                'className' => 'SafcEvent',
                'foreignKey' => 'home_id',
        ),
       'SafcAwayTeam' => array(
                'className' => 'SafcEvent',
                'foreignKey' => 'away_id',
        )
); 

在您的 SafcEvents 控制器中的操作添加和编辑中添加:

//to respect cake conventions need the select list to be plural (Teams <--- with a s at the end)
    $safcHomeTeams = $this->SafcEvent->SafcHomeTeam->find('list');
$safcAwayTeams = $this->SafcEvent->SafcAwayTeam->find('list');

//还将这些列表传递给视图 $this->set(compact('SafcAwayTeams', 'SafcHomeTeams));

在安全事件视图中添加和编辑

在表格部分:

//to respect cake convention to construct your select input properly 
//you need to refere to the list established in the controller ($SafcHomeTeams by using $SafcHomeTeam_id <--singular + "_" + "id")
        echo $this->Form->input('SafcAwayTeam_id'); 
        echo $this->Form->input('SafcHomeTeam_id');
于 2012-12-15T19:19:53.903 回答