简体   繁体   English

数据透视表是最好的解决方案吗?

[英]Is a pivot table the best solution?

I have a very basic question regarding pivot tables and best practice for a specific piece of functionality. 关于枢纽分析表和特定功能的最佳实践,我有一个非常基本的问题。

In essence, I have two entities 本质上,我有两个实体

Players Questions 玩家问题

I display a question to a player, and when they answer, I fire an event to reward them points or not depending on if they answered correctly or not. 我向玩家显示问题,当他们回答时,我会根据他们是否回答正确而触发一个事件来奖励他们积分。 What I want to do now is create a table to store what question the player has answered. 我现在想做的是创建一个表来存储玩家回答的问题。 So in the event, I can associate the player with a question. 因此,在活动中,我可以将玩家与一个问题相关联。

something like, 就像是,

$player->question()->associate($question);

This will be on a many to many relationship between the player and the question. 这将取决于玩家和问题之间的多对多关系。

my question here however is this: 我的问题是这样的:

Is this the best solution in terms of practicality and scalability, as well as, how would I go about getting the questions that the player hasn't answered in the controller. 就实用性和可扩展性而言,这是最好的解决方案吗?我将如何解决播放器在控制器中未回答的问题?

basically the opposite of 基本上是相反的

$player->questions();

If I was to use a many to many relationship 如果我要使用多对多关系

UPDATE 更新

Models PlayerProfiles 型号播放器配置文件

<?php

namespace App\Models;

use App\Traits\Pointable;
use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class PlayerProfiles
 * @package App\Models
 */
class PlayerProfiles extends Model implements Transformable
{
    use TransformableTrait, Pointable;

    /**
     * @var string
     */
    protected $modelName = 'PlayerProfiles';

    /**
     * @var array
     */
    protected $fillable = ['msisdn'];

    /**
     * @return string
     */
    public function getModelName()
    {
        return $this->modelName;
    }

    public function questions()
    {
        return $this->belongsToMany('App\Models\Questions')->withTimestamps();
    }

}

Questions 问题

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

class Questions extends Model implements Transformable
{
    use TransformableTrait, SoftDeletes;

    protected $modelName = 'Questions';

    protected $fillable = ['question', 'correct_answer', 'incorrect_answer', 'category_id', 'language_id', 'difficulty_level_id'];

    public function getModelName()
    {
        return $this->modelName;
    }

    public function playerProfiles()
    {
        return $this->belongsToMany('App\Models\PlayerProfiles')->withTimestamps();
    }
}

Migrations 移居

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePlayerQuestionsPivotTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('player_questions', function(Blueprint $table)
        {
            $table->tinyInteger('player_id')->unsigned()->nullable();
            $table->tinyInteger('question_id')->unsigned()->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('player_questions');
    }
}


<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddForeignKeysToPlayerQuestionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('player_questions', function(Blueprint $table)
        {
            $table->foreign('question_id')->references('id')->on('questions')->onUpdate('RESTRICT')->onDelete('CASCADE');
            $table->foreign('player_id')->references('id')->on('player_profiles')->onUpdate('RESTRICT')->onDelete('CASCADE');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('player_questions', function(Blueprint $table)
        {
            $table->dropForeign('player_questions_questions_id_foreign');
            $table->dropForeign('player_questions_player_id_foreign');
        });
    }
}

There are actually only 2 questions here to answer: Can a player answer to multiple questions? 实际上,这里只需要回答两个问题:玩家可以回答多个问题吗? And: Can one question be answered by many players? 并且:一个问题可以被很多玩家回答吗? If the answer is YES, then automatically you need a pivot table. 如果答案为“是”,则自动需要一个数据透视表。 That is the best practice. 那是最佳做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM