加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel自定义数据透视用法(扩展模型或扩展数据透视表)

发布时间:2020-12-14 19:37:22 所属栏目:大数据 来源:网络整理
导读:我真的不习惯使用我的支点的自定义模型,并且不明白为什么我们可以为它制作自定义模型,如果我们不能将它用作模型. 我已经看过很多答案和文章,所有人都选择扩展Pivot或Model,但是现实生活中的用法从未被考虑过,而且从来没有比给它一个“命名”类更进一步. 这是
我真的不习惯使用我的支点的自定义模型,并且不明白为什么我们可以为它制作自定义模型,如果我们不能将它用作模型.
我已经看过很多答案和文章,所有人都选择扩展Pivot或Model,但是现实生活中的用法从未被考虑过,而且从来没有比给它一个“命名”类更进一步.

这是我想要实现的一个例子:
我的主要型号:
球员模型,与球员表.
游戏模型,带游戏桌.

玩家和游戏有多对多关系,我们可以用“game_player”表称为“Party”.

现在不那么重要了,我还有一个得分/转弯的分数模型,一个Party可以有很多分数,所以得分表有一个party_id条目.

所以,这里基本上是我的课程(Laravel 5.2):

播放机

class Player extends Model
{
    // The Games this Player is participating to.
    public function games() {
        return $this->belongsToMany(Game::class)->withPivot('id')->withTimestamps();
    }

    // The Scores belonging to this Player.
    public function parties() {
        return $this->hasManyThrough(Score::class,Party::class);
    }

    // The Custom Pivot (Party) creation for this Player.
    public function newPivot(Model $parent,array $attributes,$table,$exists) {
        if ($parent instanceof Game) {
            return new Party($parent,$attributes,$exists);
        }
        return parent::newPivot($parent,$exists);
    }
}

游戏

class Game extends Model
{
    // The Players participating into this Game.
    public function players() {
        return $this->belongsToMany(Player::class)->withPivot('id')->withTimestamps();
    }

    // The Scores belonging to this Party.
    public function parties() {
        return $this->hasManyThrough(Score::class,Party::class);
    }

    // The Custom Pivot (Party) creation for this Game.
    public function newPivot(Model $parent,$exists) {
        if ($parent instanceof Player) {
            return new Party($parent,$exists);
    }
}

派对

class Party extends Pivot
{
    protected $table = 'game_player';

    // The Player this Party belongs to.
    public function player() {
        return $this->belongsTo(Player::class);
    }

    // The Game this Party belongs to.
    public function event() {
        return $this->belongsTo(Game::class);
    }

    // The Scores belonging to this Party.
    public function scores()
    {
        return $this->hasMany(Score::class);
    }
}

得分了

class Score extends Model
{
    // The Party this Score belongs to (has "party_id" column).
    public function party() {
        return $this->belongsTo(Party::class);
    }
}

现在,我有两组不同的问题,取决于我是否在Party上扩展Model或Pivot:

1)当Party扩展Pivot时:

>不能做类似Party :: count()的事情,任何适用于模型的东西.
>不能做像Party :: where(“player_id”,$player-> id)之类的东西……基本上我希望派对上有一个得分/转牌表并且这样做我需要选择枢轴模型.
>不能做类似$events-> player-> first() – >得分()或事件$events-> player-> first() – > pivot-> scores(),它突破了以下内容:

PHP error: Argument 1 passed to IlluminateDatabaseEloquentRelationsPivot::__construct() must be an instance of IlluminateDatabaseEloquentModel

2)当Party扩展模型时:

>可以做一些琐碎的事情,比如Party :: count(),Party :: where(“Game_id”,$game-> id)
>失去所有的枢轴功能,所以不能做$game->玩家,甚至不是$game-> player()工作:$game-> players() – > count()是正确的,但$game – > players() – > first()或$game-> players-> first()将突破以下内容:

PHP error: Argument 1 passed to IlluminateDatabaseEloquentModel::__construct() must be of the type array,object given

题:

如何在实例化时正确使用自定义枢轴模型是不同的?

我希望实现以下目标:

>做正常的$events->球员 – >附加($player)
>能够附加分数,如$score-> party_id = $events-> players-> find($x) – > pivot-> id(或通过范围的任何其他方式)
>能够计算正在玩的所有方,或某个特定游戏或特定玩家的方(如Party :: count(),$player-> parties-> count()等)

谢谢.

解决方法

对于我对laravel的一点体验,我发现当你有多对多关系时只使用表A中的特定对象与表B之间保持一个链接时,就会使用数据透视表.例如,带角色的用户.

由于您可以成像,因此用户A只需要与角色B建立一个链接/关系.
但是,如果我们想要使用需要在userA和productB之间保留不同记录的数据透视表(例如Sales),会发生什么?我没有找到直接的方法来做到这一点.

我的解决方案是将intermadiate表视为一个实际模型,然后与两个表(用户 – 销售和产品 – 销售)建立一对多的关系,然后,如果我需要,将这个中间模型与另一个模型联系起来.

所以在你的情况下,我有4个明确的模型:

>播放器|与党一对多
>游戏|与党一对多
>派对|与分数一对一|与党多对一|与玩家多对一
>得分|与党一对一

使用这种方法,您还可以更好地控制每个模型的属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读