Cakephp 3 多个关联相同的模型

Cakephp 3 multiple associations same model

我正在制作游戏。现在我有 3 个 table、'games'、'users' 和 'games_users'。对于 'games' 和 'users' 我烘焙了所有文件 'games_users' 的关联工作正常。

这是我的table:

CREATE TABLE `games` (
`id` int(11) NOT NULL,
`owner` int(11) NOT NULL,
`userturn` int(11) NOT NULL,
`tag` varchar(20) NOT NULL,
`status` int(1) NOT NULL,
`gametype_id` int(11) NOT NULL,
`won` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `users` (
`id` int(11) NOT NULL,
`facebook` varchar(20) NOT NULL,
`first_name` varchar(20) NOT NULL,
`last_name` varchar(20) NOT NULL,
`username` varchar(20) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `games_users` (
`game_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我对 games_users 的联想:

在 GamesTable 中:

$this->belongsToMany('Users', [
        'foreignKey' => 'game_id',
        'targetForeignKey' => 'user_id',
        'joinTable' => 'games_users'
]);

在用户表中:

$this->belongsToMany('Games', [
        'foreignKey' => 'user_id',
        'targetForeignKey' => 'game_id',
        'joinTable' => 'games_users'
]);

在table'games'中有两列'userturn'和'won',它们都是users_id的。我如何为这些列建立关联?

您可以为游戏 table 中的用户 table 使用不同的用户别名设置多个关联。

GamesTable中:

$this->belongsTo('UserTurn', [
    'className' => 'Users',
    'foreignKey' => 'userturn'
]);

$this->belongsTo('Won', [
    'className' => 'Users',
    'foreignKey' => 'won'
]);