哪种 cakephp 关系应该用于用户组? haveMany 可以使用数组作为foreign_ID 吗?

发布于 2024-08-23 20:19:00 字数 751 浏览 8 评论 0原文

在我的网球应用程序中,我的“比赛”表有两名球员(显然)。我使用player1_id 和player2_id 作为跟踪用户ID 的方法。不过我还没有包含 user_id 外键。

还有一个“用户”表,我想从中提取玩家的姓名。

因为“match”有多个用户,并且用户有多个模型,所以我想知道以下模型配置是否有效:

我已经像这样设置了用户模型:

var $name = 'User';
var $hasMany = array(
'Match' => array(
'className' => 'Match',
'foreignKey' => array( 'player1_id', 'player2_id'),
'dependent' => true,
)

并且像这样的匹配模型:

var $name = 'Match';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' =>  'user_id',
'conditions' => '',
)

我需要显示每个玩家的用户名字/姓氏,其中 user_id =player1_id 或player2_id。这行得通吗?外键可以使用数组吗?模型运算在搜索时可以使用“或”吗?

或者是否有更好的方法来构建具有多个用户的表格(例如一场比赛,或者可能是一次小组会议)?

干杯, 保罗

In my tennis application, my 'match' table has two players (obviously). I've used player1_id and player2_id as a way to track the user ids. I haven't included a user_id foreign key though.

There is also a 'user' table where I want to pull player's names from.

Because 'match' has more than one user and users have more than one model, I'm wondering if the following model configuration will work:

I've setup the user model like this:

var $name = 'User';
var $hasMany = array(
'Match' => array(
'className' => 'Match',
'foreignKey' => array( 'player1_id', 'player2_id'),
'dependent' => true,
)

and the match model like this:

var $name = 'Match';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' =>  'user_id',
'conditions' => '',
)

I need to display the user first/last name for each player where user_id = player1_id or player2_id. Will this work? Can a foreign key use an array? And can a model operation use an 'or' when searching?

Or is there a better way to structure a table (like a match, or could be a group meeting) with more than one user?

Cheers,
Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏九 2024-08-30 20:19:00

我不认为数组作为 User > 上的外键匹配关系会起作用,但话又说回来,我从未尝试过。单场比赛>但是,与 user_id 作为外部的用户关系将不起作用,因为正如您所说,该外部 id 不存在。

从概念上讲,最干净的方法是正确的 hasAndBelongsToMany 关系。毕竟,一场比赛有很多玩家,而玩家也有很多场比赛。所以你实际上正在寻找多对多关系。

要使用“belongsTo/hasMany”关系来伪造它,您需要执行以下操作:

// user model
var $hasMany = array(
   'MatchAsPlayer1' => array(
       'className'  => 'Match',
       'foreignKey' => 'player1_id',
   ),
   'MatchAsPlayer2' => array(
       'className'  => 'Match',
       'foreignKey' => 'player2_id',
   )
);

// match model
var $belongsTo = array(
    'Player1' => array(
        'className'  => 'User',
        'foreignKey' =>  'player1_id'
    ),
    'Player2' => array(
        'className'  => 'User',
        'foreignKey' =>  'player2_id'
    )
);

丑陋的部分位于用户模型中,您将在其中收到分成 ['MatchAsPlayer1']的相关匹配项>['MatchAsPlayer2']。您可以在 afterFind 回调中执行一些技巧来合并这些内容,但这总体上不是一个很好的解决方案。只需选择 hasAndBelongsToMany 即可。 :)

I don't think an array as a foreign key on the User > Match relationship would work, but then again I have never tried. The single Match > User relationship with user_id as the foreign won't work though, since, as you said, that foreign id does not exist.

Conceptually the cleanest way would be a proper hasAndBelongsToMany relationship. After all, a match has many players, and players have many matches. So you're really looking at a many-to-many relationship.

To fake it with a belongsTo/hasMany relationship, you would need to do this:

// user model
var $hasMany = array(
   'MatchAsPlayer1' => array(
       'className'  => 'Match',
       'foreignKey' => 'player1_id',
   ),
   'MatchAsPlayer2' => array(
       'className'  => 'Match',
       'foreignKey' => 'player2_id',
   )
);

// match model
var $belongsTo = array(
    'Player1' => array(
        'className'  => 'User',
        'foreignKey' =>  'player1_id'
    ),
    'Player2' => array(
        'className'  => 'User',
        'foreignKey' =>  'player2_id'
    )
);

The ugly part is in the User model, where you'd receive related matches split into ['MatchAsPlayer1'] and ['MatchAsPlayer2']. You could do some trickery in the afterFind callback to merge those, but it's not a nice solution overall. Just go for the hasAndBelongsToMany. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文